關於JSP裡JavaBean的用法分析

來源:互聯網
上載者:User

關於JSP裡JavaBean的用法分析:我們可以使用Scriptlet的方式: <%! %> / <% %>

還可以用<jsp:useBean>的方式//bean.HelloBean.java:

package bean;public class HelloBean {private String str;public void setStr(String s){str = s;}public String getStr(){return str;}}

//index.jsp

<%@page import="bean.HelloBean"%><html>  <body>    <%!bean.HelloBean memBean = new bean.HelloBean();%>    <%memBean.setStr("memBean!!"); %>    <%=memBean.getStr()%>     <%bean.HelloBean partBean = new bean.HelloBean();%>    <%partBean.setStr("partBean!!"); %>    <%=partBean.getStr()%>        <jsp:useBean id="pageBean" scope="page" class="bean.HelloBean">    </jsp:useBean>        <%pageBean.setStr("pageBean!!"); %>    <%=pageBean.getStr()%>     <jsp:useBean id="requestBean" scope="request" class="bean.HelloBean">    </jsp:useBean>        <%requestBean.setStr("pageBean!!"); %>    <%=pageBean.getStr()%>        <jsp:useBean id="sessionBean" scope="session" class="bean.HelloBean">    </jsp:useBean>        <%sessionBean.setStr("pageBean!!"); %>    <%=sessionBean.getStr()%>        <jsp:useBean id="appBean" scope="application" class="bean.HelloBean">    </jsp:useBean>        <%sessionBean.setStr("appBean!!"); %>    <%=sessionBean.getStr()%>      </body></html>

jsp:useBean各項參數含義:id:對象執行個體名稱scope:Bean作用的範圍,

預設為page,對整個jsp頁面有效

Scope各項參數的意義:page:僅涵蓋使用JavaBean的頁面(PageBean.jsp/CounterBean.java)

request:有效範圍僅限於使用JavaBean的請求(RequestBean.jsp/RequestBean2.jsp/CounterBean.java)

session:有效範圍在使用者整個串連過程中(整個會話階段均有效)(SessionBean.jsp/Session2.jsp/CounterBean.java)

application:有效範圍涵蓋整個應用程式。也就是對整個web應用均有效(Application.jsp/Application2.jsp/CounterBean.java)

//然後我們去到Tomcat的work目錄下查看jsp被編譯產生的java檔案:

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.22 * Generated at: 2011-11-02 01:36:52 UTC * Note: The last modified time of this file was set to *       the last modified time of the source file after *       generation to assist with modification tracking. */package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import bean.HelloBean;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {bean.HelloBean memBean = new bean.HelloBean();  //<%!bean.HelloBean memBean = new bean.HelloBean();%> <%! %> 使這個bean變數這個類的成員變數  private static final javax.servlet.jsp.JspFactory _jspxFactory =          javax.servlet.jsp.JspFactory.getDefaultFactory();  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.tomcat.InstanceManager _jsp_instancemanager;  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());  }  public void _jspDestroy() {  }  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)        throws java.io.IOException, javax.servlet.ServletException {    final javax.servlet.jsp.PageContext pageContext;    javax.servlet.http.HttpSession session = null;    final javax.servlet.ServletContext application;    final javax.servlet.ServletConfig config;    javax.servlet.jsp.JspWriter out = null;    final java.lang.Object page = this;    javax.servlet.jsp.JspWriter _jspx_out = null;    javax.servlet.jsp.PageContext _jspx_page_context = null;    try {      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("\r\n");      out.write("<html>\r\n");      out.write("  <body>\r\n");            out.write("\r\n");      out.write("    ");      out.write("\r\n");      out.write("    ");memBean.setStr("memBean!!");       out.write("\r\n");      out.write("    ");      out.print(memBean.getStr());      out.write("\r\n");      out.write(" \r\n");      out.write("    ");      bean.HelloBean partBean = new bean.HelloBean();  //<%bean.HelloBean partBean = new bean.HelloBean();%> <% %>使bean成為了類的成員函數的局部變數      out.write("\r\n");      out.write("    ");partBean.setStr("partBean!!");       out.write("\r\n");      out.write("    ");      out.print(partBean.getStr());      out.write("\r\n");      out.write("    \r\n");      out.write("    ");            bean.HelloBean pageBean = null;  //<jsp:useBean id="pageBean" scope="page" class="bean.HelloBean">      pageBean = (bean.HelloBean) _jspx_page_context.getAttribute("pageBean", javax.servlet.jsp.PageContext.PAGE_SCOPE);      if (pageBean == null){        pageBean = new bean.HelloBean();        _jspx_page_context.setAttribute("pageBean", pageBean, javax.servlet.jsp.PageContext.PAGE_SCOPE);        out.write("\r\n");        out.write("    ");      }      out.write("\r\n");      out.write("    \r\n");      out.write("    ");pageBean.setStr("pageBean!!");       out.write("\r\n");      out.write("    ");      out.print(pageBean.getStr());      out.write("\r\n");      out.write("    \r\n");      out.write(" \t");                  bean.HelloBean requestBean = null; //<jsp:useBean id="requestBean" scope="request" class="bean.HelloBean">      requestBean = (bean.HelloBean) _jspx_page_context.getAttribute("requestBean", javax.servlet.jsp.PageContext.REQUEST_SCOPE);      if (requestBean == null){        requestBean = new bean.HelloBean();        _jspx_page_context.setAttribute("requestBean", requestBean, javax.servlet.jsp.PageContext.REQUEST_SCOPE);        out.write("\r\n");        out.write("    ");      }      out.write("\r\n");      out.write("    \r\n");      out.write("    ");requestBean.setStr("pageBean!!");       out.write("\r\n");      out.write("    ");      out.print(pageBean.getStr());      out.write("\r\n");      out.write("    \r\n");      out.write("    ");                  bean.HelloBean sessionBean = null;// <jsp:useBean id="sessionBean" scope="session" class="bean.HelloBean">      synchronized (session) {        sessionBean = (bean.HelloBean) _jspx_page_context.getAttribute("sessionBean", javax.servlet.jsp.PageContext.SESSION_SCOPE);        if (sessionBean == null){          sessionBean = new bean.HelloBean();          _jspx_page_context.setAttribute("sessionBean", sessionBean, javax.servlet.jsp.PageContext.SESSION_SCOPE);          out.write("\r\n");          out.write("    ");        }      }      out.write("\r\n");      out.write("    \r\n");      out.write("    ");sessionBean.setStr("pageBean!!");       out.write("\r\n");      out.write("    ");      out.print(sessionBean.getStr());      out.write("\r\n");      out.write("    \r\n");      out.write("    ");                  bean.HelloBean appBean = null;  // <jsp:useBean id="appBean" scope="application" class="bean.HelloBean">      synchronized (application) {        appBean = (bean.HelloBean) _jspx_page_context.getAttribute("appBean", javax.servlet.jsp.PageContext.APPLICATION_SCOPE);        if (appBean == null){          appBean = new bean.HelloBean();          _jspx_page_context.setAttribute("appBean", appBean, javax.servlet.jsp.PageContext.APPLICATION_SCOPE);          out.write("\r\n");          out.write("    ");        }      }      out.write("\r\n");      out.write("    \r\n");      out.write("    ");sessionBean.setStr("appBean!!");       out.write("\r\n");      out.write("    ");      out.print(sessionBean.getStr());      out.write("\r\n");      out.write("    \r\n");                  out.write("  </body>\r\n");      out.write("</html>\r\n");    } catch (java.lang.Throwable t) {      if (!(t instanceof javax.servlet.jsp.SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          try { out.clearBuffer(); } catch (java.io.IOException e) {}        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);      }    } finally {      _jspxFactory.releasePageContext(_jspx_page_context);    }  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.