1 / 10

J2EE —— 第 16 章 JSP 页中的脚本

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() {

alaina
Download Presentation

J2EE —— 第 16 章 JSP 页中的脚本

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. J2EE——第16章JSP页中的脚本

  2. 使用脚本 <%@ 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; } %>

  3. scriptlet <% scripting language statements %> <% String username = request.getParameter("username"); if ( username != null && username.length() > 0 ) { %> <%@include file="response.jsp" %> <% } %>

  4. 表达式 <%= 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>

  5. 编写接受脚本元素的标签处理程序 <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();

  6. 具有体的标签 • 没有操纵体的标签处理程序 • 实现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:依赖于体内容的所有初始化

  7. 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方法中重置状态,释放私有资源

  8. 协作标签(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(); ...} } }

  9. 协作标签(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>

  10. 定义变量的标签 <ws:hello var="response“ name="<%=request.getParameter("username")%>" /> <h2><font color="black"><%= response %>!</font></h2>

More Related