100 likes | 218 Views
J2EE —— 第 16 章 JSP 页中的脚本. 使用脚本. <%@ page language="scripting language" %> <%@ page import="fully_qualified_classname, packagename.*" %> <%@ page import="javax.xml.rpc.Stub,webclient.*" %> <%! scripting language declaration %> <%! private BookDBAO bookDBAO; public void jspInit() {
E N D
使用脚本 <%@ page language="scripting language" %> <%@ page import="fully_qualified_classname, packagename.*" %> <%@ page import="javax.xml.rpc.Stub,webclient.*" %> <%! scripting language declaration %> <%! private BookDBAO bookDBAO; public void jspInit() { bookDBAO = (BookDBAO)getServletContext().getAttribute("bookDB"); if (bookDBAO == null) System.out.println("Couldn’t get database."); }%> <%! public void jspDestroy() { bookDBAO = null; } %>
scriptlet <% scripting language statements %> <% String username = request.getParameter("username"); if ( username != null && username.length() > 0 ) { %> <%@include file="response.jsp" %> <% } %>
表达式 <%= scripting language expression %> <% String resp = null; try { Stub stub = (Stub)(new MyHelloService_Impl().getHelloIFPort()); stub._setProperty( javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8080/hello-jaxrpc/hello"); HelloIF hello = (HelloIF)stub; resp = hello.sayHello(request.getParameter("username")); } catch (Exception ex) { resp = ex.toString(); } %> <h2><font color="black"><%= resp %>!</font></h2>
编写接受脚本元素的标签处理程序 <body-content>empty | JSP | tagdependent</body-content> • Tag接口 ATag t = new ATag(); t.setPageContext(...); t.setParent(...); t.setAttribute1(value1); t.setAttribute2(value2); t.doStartTag(); t.doEndTag(); t.release(); • BodyTag接口 t.doStartTag(); out = pageContext.pushBody(); t.setBodyContent(out); t.doInitBody(); t.doAfterBody(); // while doAfterBody returns EVAL_BODY_AGAIN we iterate body // evaluation ... t.doAfterBody(); t.doEndTag(); out = pageContext.popBody(); t.release();
具有体的标签 • 没有操纵体的标签处理程序 • 实现Tag接口或继承TagSupport • 计算标签体:doStartTag返回EVAL_BODY_INCLUDE • 不计算标签体:doStartTag返回SKIP_BODY • 迭代计算体:实现IterationTag接口 • 再次计算体:返回EVAL_BODY_AGAIN • 操纵体的标签处理程序 • 实现BodyTag接口或继承BodyTagSupport • 计算标签体:doStartTag返回EVAL_BODY_BUFFERED • 不计算标签体:doStartTag返回SKIP_BODY • 再次计算体:doAfterBody返回EVAL_BODY_AGAIN • 不需要重新计算体:doAfterBody返回SKIP_BODY • doInitBody:依赖于体内容的所有初始化
doAfterBody public class QueryTag extends BodyTagSupport { public int doAfterBody() throws JspTagException { BodyContent bc = getBodyContent(); String query = bc.getString(); bc.clearBody(); try { Statement stmt = connection.createStatement(); result = stmt.executeQuery(query); } catch (SQLException e) { throw new JspTagException("QueryTag: " + e.getMessage()); } return SKIP_BODY; } } • 在release方法中重置状态,释放私有资源
协作标签(1) public class QueryTag extends BodyTagSupport { public int doStartTag() throws JspException { String cid = getConnectionId(); Connection connection; if (cid != null) { connection =(Connection)pageContext. getAttribute(cid); } else { ConnectionTag ancestorTag = (ConnectionTag)findAncestorWithClass(this, ConnectionTag.class); if (ancestorTag == null) { throw new JspTagException("A query without a connection attribute must be nested within a connection tag."); } connection = ancestorTag.getConnection(); ...} } }
协作标签(2) <tt:connection cid="con01" ... >... </tt:connection> <tt:query id="balances" connectionId="con01"> SELECT account, balance FROM acct_table where customer_number = ? <tt:param value="${requestScope.custNumber}" /> </tt:query> <tt:connection ... > <tt:query cid="balances"> SELECT account, balance FROM acct_table where customer_number = ? <tt:param value="${requestScope.custNumber}" /> </tt:query> </tt:connection> <tag>...<attribute><name>connectionId</name> <required>false</required> </attribute></tag>
定义变量的标签 <ws:hello var="response“ name="<%=request.getParameter("username")%>" /> <h2><font color="black"><%= response %>!</font></h2>