實作目標:在 jsp 內執行 scripting language。
選用語言:Lua (最近好像蠻多人討論的)
流程:
1. 製作一個含 TagBodySupport 的 Tag Class (含 tld, 與 taglib 宣告)
2. 製作 script 執行類別 (LuaInvoker)
3. 試用 (tag.jsp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| package cc.qrtt1.taglib; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.DynamicAttributes; import org.keplerproject.luajava.LuaException; import org.keplerproject.luajava.LuaState; import org.keplerproject.luajava.LuaStateFactory; public class ScriptInvokerTag extends BodyTagSupport implements DynamicAttributes { Map map = new HashMap(); String script; @Override public int doAfterBody() throws JspException { BodyContent bodyContent = getBodyContent(); try { script = bodyContent.getString(); bodyContent.clear(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SKIP_BODY; } public void setDynamicAttribute(String url, String key, Object value) throws JspException { map.put(key, value); } @Override public int doEndTag() throws JspException { String lang = (String) map.get("lang"); System.out.println("Start: "+System.currentTimeMillis()); if("lua".equalsIgnoreCase(lang)){ LuaInvoker.invoke(this, script); } System.out.println("END: "+System.currentTimeMillis()); return EVAL_PAGE; } public PageContext getPageContext(){ return pageContext; } } |
tld 宣告,基於懶惰的理由我們使用新一點的 schema 支援 dynamic attr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| <?xml version="1.0" encoding="UTF-8" ?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description> A tag library exercising SimpleTag handlers. </description> <tlib-version>1.0</tlib-version> <short-name>script</short-name> <uri>/script</uri> <tag> <description>Outputs Hello, World</description> <name>script</name> <tag-class>cc.qrtt1.taglib.ScriptInvokerTag</tag-class> <body-content>tagdependent</body-content> <dynamic-attributes>true</dynamic-attributes> </tag> </taglib> |
web.xml 加入 taglib 宣告
1
2
3
4
5
6
7
8
| <taglib> <taglib-uri> /lua </taglib-uri> <taglib-location> /WEB-INF/tld/LuaInvoker.tld </taglib-location> </taglib> |
建立 LuaInvoker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| package cc.qrtt1.taglib; import org.keplerproject.luajava.LuaException; import org.keplerproject.luajava.LuaState; import org.keplerproject.luajava.LuaStateFactory; public class LuaInvoker { public static void invoke(ScriptInvokerTag tag, String script) { try { LuaState lua = LuaStateFactory.newLuaState(); lua.openLibs(); lua.pushObjectValue(tag.getPageContext().getOut()); lua.setGlobal("out"); int err = lua.LdoString(script); lua.close(); } catch (LuaException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
建立測試的 jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="script" uri="/script" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Scripting In JSP</title> </head> <body> <script:script lang="lua"> out:write("write something from lua script <br>") i = 10 while i > 0 do i = i - 1 j = 0 repeat out:write("*") j = j + 1 until j > i out:write("<br>") end </script:script> </body> </html> |
沒有留言:
張貼留言