Java Web基礎:第六講 JSP運行原理

來源:互聯網
上載者:User

JSP的運行原理如所示:下面看看JSP檔案在各個階段的內容。源檔案:success.jsp<%@ page contentType="text/html;charset=gb2312"%><html>   <head>      <title>登入成功</title>   </head>   <body>      <h2>${sessionScope.userid}您好,歡迎登入網上書店!</h2>   </body></html> 與Servlet的運行原理不同的是,JSP需要先轉換成Java檔案。success.jsp檔案被轉換成的Java檔案的內容如下(位於Tomcat安裝目錄下的work/Catalina/localhost/ch2/org/apache/jsp檔案夾中,ch2是我的應用程式的名字):package org.apache.jsp; import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*; public final class success_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static java.util.Vector _jspx_dependants;  public java.util.List 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;charset=gb2312");      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("   <head>/r/n");      out.write("      <title>登入成功</title>/r/n");      out.write("   </head>/r/n");      out.write("   <body>/r/n");      out.write("      <h2>");      out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${sessionScope.userid}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));      out.write("您好,歡迎登入網上書店!</h2>/r/n");      out.write("   </body>/r/n");      out.write("</html>/r/n");    } 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);    } }}從JSP被轉換成的Java檔案可以看出如下幾點:1)  JSP檔案中的內容基本都被包含在了_jspService方法中,實際上頁面執行的過程就是這個方法執行的過程;2)  頁面中顯示給使用者的HTML資訊都被轉換成了out.println("XXXX")的形式;3)  在_jspService方法中有兩個參數request和response,4) 在方法中產生了如下幾個對象:

    PageContext pageContext = null;    HttpSession session = null;    ServletContext application = null;    ServletConfig config = null;    JspWriter out = null;    Object page = this;

這就是傳說中的內建對象(預定義對象)。返回給用戶端的代碼(通過在用戶端瀏覽器可以查看源檔案): <html>   <head>      <title>登入成功</title>   </head>   <body>      <h2>zhangsan您好,歡迎登入網上書店!</h2>   </body></html>在此檔案中看不到任何JSP的代碼,而是純HTML代碼。與源檔案不同的地方:n   源檔案中的page指令沒有了n   源檔案中的${sessionScope.userid}沒有了,而使用zhangsan代替了原來的運算式。 瀏覽器把這段HTML代碼解析成介面顯示給使用者。 這就是從你編寫的JSP檔案到用戶端看到的結果的轉換過程。 上一講:第五講 手動構建Web應用下一講:第七講 Servlet運行原理李緒成 CSDN Blog:http://blog.csdn.net/javaeeteacher邀請您為好友:http://student.csdn.net/invite.php?u=124362&c=7be8ba2b6f3b6cc5

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.