◎ Scripting Elements 描述語言元素 ◎
JSP的Scripting Elements 包含三部份:
1. Scriptlets (程式碼區段):以<% 與 %>包括的元素
<html><body><center> This is Test App<br> <% String str1; int int1; %> <% str1 = "This is a aaa!"; int1 = 10; %> Output str1 is: <%=str1%> <br> Output int1 is: <%=int1%> <br> </center></body></html> |
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class TestApp_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<html><body><center> \r\n"); out.write("This is Test App<br>\r\n"); String str1; int int1; out.write("\r\n"); out.write(" \r\n"); str1 = "This is a aaa!"; int1 = 10; out.write("\r\n"); out.write("Output str1 is: "); out.print(str1); out.write(" <br>\r\n"); out.write("Output int1 is: "); out.print(int1); out.write(" <br>\r\n"); out.write("</center></body></html>"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } } |
2. Declarations (宣告):以<%! 與 %>包括的元素
在JSP中<% 與 %>間 宣告的變數,將轉換為Servlet的方法中一個區域變數,而<%!與%>間宣告的變數,它在 Servlet中會對應至一個 field 成員,此屬性的有效範圍是整個Servlet類別產生的實例,因此,屬性是一個實例變數。 由於Servlet被載入後,將會一直存在於JVM中,直到容器規定的存在期限過期,或是關閉伺服器後才會清除Servlet產生的實例,故使用<%!與% >宣告變數時,必須小心資料共用與執行緒安全的問題。
<%!與%>之間也可以宣告method,而它將在轉換為Servlet之後, 成為Servlet類別的一個method member。
<html><body><center> This is Test App<br> <%! String str1; int int1; %> <% str1 = "This is a aaa!"; int1 = 10; %> Output str1 is: <%=str1%> <br> Output int1 is: <%=int1%> <br> </center></body></html> |
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class TestApp_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String str1; int int1; private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<html><body><center> \r\n"); out.write("This is Test App<br>\r\n"); out.write("\r\n"); out.write(" \r\n"); str1 = "This is a aaa!"; int1 = 10; out.write("\r\n"); out.write("Output str1 is: "); out.print(str1); out.write(" <br>\r\n"); out.write("Output int1 is: "); out.print(int1); out.write(" <br>\r\n"); out.write("</center></body></html>"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } |
利用 <%! …… %> 敘述宣告函數時,該函數將成為 servlet 的方法,定義語法如下:
<html><body><center> This is Test App<br> <%! 回傳資料型別 函數名稱(引數串列){ …… //程式碼 return 回傳值; } %> <% str1 = "This is a aaa!"; int1 = 10; %> Output str1 is: <%=str1%> <br> Output int1 is: <%=int1%> <br> </center></body></html> |
3. Expressions (運算式):以<%= 與 %>包括的元素,注意這個表示法中最後不需要加上分號!
<%= expression %>是一個運算式表示,它可以視作 <% out.println(expression); %> 的一個簡單表示。
例如:
<% out.println(new java.util.Date()); %>
<%= new java.util.Date() %>
<%=new out.println ("大家好")%>
<% out.println ("大家好");%>
若在web.xml中加上<script-invalid>標籤,表示設定所有的JSP網頁都不可以使用Scriptlet
... <web-app ..> .... <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <script-invalid>true</script-invalid> </jsp-property-group> </jsp-config> .... </web-app> |
留言列表