歡迎您光臨本站 註冊首頁

·JSP轉譯成Servlet詳細過程

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

很多人都會認為JSP的執行性能會和Servlet相差很多,其實執行性能上的差別只在第一次的執行.因為JSP在執行第一次后,會被編譯成Servlet的類文件,即.class,當再重複調用執行時,就直接執行第一次所產生的Servlet,而不再重新把JSP編譯成Servelt.

因此,除了第一次的編譯會花較久的時間之外,之後JSP和Servlet的執行速度就幾乎相同了.Web容器處理JSP文件請求的執行過程主要包括以下4個部分:

1.客戶端發出Request請求

2.JSP Container 將JSP轉譯成Servlet的源代碼

3.將產生的Servlet源代碼經過編譯后,並載入到內存執行

4.把結果Response(響應)至客戶端

在執行JSP網頁時,通常可以分為兩個時期:轉譯時期(Translation Time)和請求時期(Request Time).

◆轉譯時期:JSP網頁轉移成Servlet類.

◆請求時期:Servlet類執行后,響應結果至客戶端.

轉譯期間做了兩件事情:

◆轉譯時期:將JSP網頁轉移為Servlet源代碼 .java.

◆編譯時期:將Servlet 源代碼 .java編譯成 Servlet類 .class.

當JSP網頁在執行時,JSP Container會做檢查工作,如果發現JSP網頁有更新修改時,JSP Container才會再次編譯JSP成Servlet; 如果JSP沒有更新時,就直接執行前面所產生的Servlet.

  1. (showdate.jsp)
  2. <%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>
  3. <html>
  4. <head>
  5. <title>Show time</title>
  6. </head>
  7. <body

    >
  8. Hello :
  9. <%
  10. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  11. String str = format.format(new Date());
  12. %>
  13. <%=str %>
  14. </body>
  15. </html>

當部署好 showdate.jsp之後,啟動Tomcat伺服器.

1.在IE瀏覽器中輸入配置好的路徑 .... showdate.jsp 請求這個頁面.

2.JSP Container 即Tomcat 伺服器會將 showdate.jsp 轉譯成 showdate_jsp.java 源文件.

3.同時將 showdate_jsp.java 源文件編譯成 showdate_jsp.class.

4.編譯執行showdate_jsp.class 類,處理請求,返迴響應,容器將生成的頁面返回給客戶端顯示.

  1. (轉移成的java源文件 showdate_jsp.java)
  2. package org.apache.jsp.ch04;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import javax.servlet.jsp.*;
  6. import java.text.*;
  7. import java.util.*;;
  8. public final class showdate_jsp extends org.apache.jasper.runtime.HttpJspBase
  9. implements org.apache.jasper.runtime.JspSourceDependent {
  10. private static java.util.List _jspx_dependants;
  11. public Object getDependants() {
  12. return _jspx_dependants;
  13. }
  14. public void _jspService(HttpServletRequest request, HttpServletResponse response)
  15. throws java.io.IOException, ServletException {
  16. JspFactory _jspxFactory

    = null;
  17. PageContext pageContext = null;
  18. HttpSession session = null;
  19. ServletContext application = null;
  20. ServletConfig config = null;
  21. JspWriter out = null;
  22. Object page = this;
  23. JspWriter _jspx_out = null;
  24. PageContext _jspx_page_context = null;
  25. try {
  26. _jspxFactory = JspFactory.getDefaultFactory();
  27. response.setContentType("text/html;charset=gb2312");
  28. pageContext = _jspxFactory.getPageContext(this, request, response,
  29. null, true, 8192, true);
  30. _jspx_page_context = pageContext;
  31. application = pageContext.getServletContext();

  32. config = pageContext.getServletConfig();
  33. session = pageContext.getSession();
  34. out = pageContext.getOut();
  35. _jspx_out = out;
  36. out.write("rn");
  37. out.write("<html>rn");
  38. out.write("<head>rn");
  39. out.write("<title>Show time</title>rn");
  40. out.write("</head>rn");
  41. out.write("<body> rn");
  42. out.write("tHello : rn");
  43. out.write("t");
  44. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  45. String str = format.format(new Date());
  46. out.write("rn");
  47. out.write("t ");
  48. out.print(str );
  49. out.write("rn");
  50. out.write("</body>rn");
  51. out.write("</html

    >
    ");
  52. } catch (Throwable t) {
  53. if (!(t instanceof SkipPageException)){
  54. out = _jspx_out;
  55. if (out != null && out.getBufferSize() != 0)
  56. out.clearBuffer();
  57. if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
  58. }
  59. } finally {
  60. if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
  61. }
  62. }
  63. }

當JSP頁面被轉譯成Servlet時,內容主要包含三個部分:

  1. public void _jspInit(){ ..}
  2. -- 當JSP網頁一開始執行時,最先執行此方法,執行初始化工作
  3. public void _jspDestory(){...} – JSP網頁執行的方法
  4. public void _jspService(HttpServletRequest request, HttpServletResponse response)
  5. throws java.io.IOException, ServletException {

JSP網頁中最主要的程序都是在此執行,將showdate.jsp和showdate_jsp.java做一個簡單對比:

第一部分:頁面屬性的對比

  1. <%@ page language="java" contentType="text/html;charset=gb2312" %>
  2. response.setContentType("text/html;charset=gb2312");
  3. //通過 response響應設置返回客戶端的頁面屬性

第二部分:HTML標籤

  1. <html>
  2. <head>
  3. <title>
    Show time</title>
  4. </head>
  5. ..
  6. </html>
  7. out.write("rn");
  8. out.write("<html>rn");
  9. out.write("<head>rn");
  10. out.write("<title>Show time</title>rn");
  11. out.write("</head>rn");
  12. out.write("<body> rn");
  13. out.write("tHello : rn");
  14. out.write("t");
  15. //通過 out對象 向客戶端寫HTML標籤

第三部分:聲明的對象

  1. <%
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  3. String str = format.format(new Date());
  4. %>

在_jspService 方法中聲明的局部變數:

  1. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  2. String str = format

    .format(new Date());

第四部分:表達式

  1. <%=str %>
  2. out.print(str ); //寫即列印str變數的值


[火星人 ] ·JSP轉譯成Servlet詳細過程已經有803次圍觀

http://coctec.com/docs/java/show-post-60087.html