`

JSP转译成Servlet过程

阅读更多

JSP 编译和运行过程与JSP转移源码简单分析


Web容器处理JSP文件请求的执行过程主要包括以下4个部分:
1.      客户端发出Request请求
2.      JSP Container 将JSP转译成Servlet的源代码
3.      将产生的Servlet源代码经过编译后,并加载到内存执行
4.      把结果Response(响应)至客户端

很多人都会认为JSP的执行性能会和Servlet相差很多,其实执行性能上的差别只在第一次的执行。因为JSP在执行第一次后,会被编译成Servlet的类文件,即.class,当再重复调用执行时,就直接执行第一次所产生的Servlet,而不再重新把JSP编译成Servelt。因此,除了第一次的编译会花较久的时间之外,之后JSP和Servlet的执行速度就几乎相同了。


在执行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.

showdate.jsp

Java代码 复制代码
  1. <%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>   
  2. <html>   
  3. <head>   
  4. <title>Show time</title>   
  5. </head>   
  6. <body>    
  7.      Hello :    
  8.      <%   
  9.          SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");   
  10.          String str = format.format(new Date());   
  11.       %>   
  12.       <%=str %>   
  13. </body>   
  14. </html>  
<%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>
<html>
<head>
<title>Show time</title>
</head>
<body> 
     Hello : 
     <%
         SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
         String str = format.format(new Date());
      %>
      <%=str %>
</body>
</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 类,处理请求,返回响应,容器将生成的页面返回给客户端显示

转移成的 java 源文件  showdate_jsp.java

Java代码 复制代码
  1. package org.apache.jsp.ch04;   
  2.   
  3. import javax.servlet.*;   
  4. import javax.servlet.http.*;   
  5. import javax.servlet.jsp.*;   
  6. import java.text.*;   
  7. import java.util.*;;   
  8.   
  9. public final class showdate_jsp extends org.apache.jasper.runtime.HttpJspBase   
  10.     implements org.apache.jasper.runtime.JspSourceDependent {   
  11.   
  12.   private static java.util.List _jspx_dependants;   
  13.   
  14.   public Object getDependants() {   
  15.     return _jspx_dependants;   
  16.   }   
  17.   
  18.   public void _jspService(HttpServletRequest request, HttpServletResponse response)   
  19.         throws java.io.IOException, ServletException {   
  20.   
  21.     JspFactory _jspxFactory = null;   
  22.     PageContext pageContext = null;   
  23.     HttpSession session = null;   
  24.     ServletContext application = null;   
  25.     ServletConfig config = null;   
  26.     JspWriter out = null;   
  27.     Object page = this;   
  28.     JspWriter _jspx_out = null;   
  29.     PageContext _jspx_page_context = null;   
  30.   
  31.     try {   
  32.       _jspxFactory = JspFactory.getDefaultFactory();   
  33.       response.setContentType("text/html;charset=gb2312");   
  34.       pageContext = _jspxFactory.getPageContext(this, request, response,   
  35.                    nulltrue8192true);   
  36.       _jspx_page_context = pageContext;   
  37.       application = pageContext.getServletContext();   
  38.       config = pageContext.getServletConfig();   
  39.       session = pageContext.getSession();   
  40.       out = pageContext.getOut();   
  41.       _jspx_out = out;   
  42.   
  43.       out.write("\r\n");   
  44.       out.write("<html>\r\n");   
  45.       out.write("<head>\r\n");   
  46.       out.write("<title>Show time</title>\r\n");   
  47.       out.write("</head>\r\n");   
  48.       out.write("<body> \r\n");   
  49.       out.write("\tHello : \r\n");   
  50.       out.write("\t");   
  51.   
  52.          SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");   
  53.          String str = format.format(new Date());   
  54.   
  55.       out.write("\r\n");   
  56.       out.write("\t ");   
  57.       out.print(str );   
  58.       out.write("\r\n");   
  59.       out.write("</body>\r\n");   
  60.       out.write("</html>");   
  61.     } catch (Throwable t) {   
  62.   
  63.       if (!(t instanceof SkipPageException)){   
  64.         out = _jspx_out;   
  65.         if (out != null && out.getBufferSize() != 0)   
  66.           out.clearBuffer();   
  67.         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);   
  68.       }   
  69.   
  70.     } finally {   
  71.   
  72.       if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);   
  73.     }   
  74.   }   
  75. }  
package org.apache.jsp.ch04;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.text.*;
import java.util.*;;

public final class showdate_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static java.util.List _jspx_dependants;

  public Object 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>Show time</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body> \r\n");
      out.write("\tHello : \r\n");
      out.write("\t");

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

      out.write("\r\n");
      out.write("\t ");
      out.print(str );
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } 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 页面被转译成Servlet时,内容主要包含三个部分:

Java代码 复制代码
  1. public void _jspInit(){ ..}     -- 当JSP网页一开始执行时,最先执行此方法,执行初始化工作   
  2.   
  3. public void _jspDestory(){...} – JSP网页最后执行的方法   
  4.   
  5. public void _jspService(HttpServletRequest request, HttpServletResponse response)   
  6.         throws java.io.IOException, ServletException {  
public void _jspInit(){ ..}     -- 当JSP网页一开始执行时,最先执行此方法,执行初始化工作

public void _jspDestory(){...} – JSP网页最后执行的方法

public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {


-- JSP网页中最主要的程序都是在此执行

将 showdate.jsp 和 showdate_jsp.java 做一个简单对比


第一部分:页面属性的对比

Java代码 复制代码
  1. <%@ page language="java" contentType="text/html;charset=gb2312" %>   
  2. response.setContentType("text/html;charset=gb2312");    
<%@ page language="java" contentType="text/html;charset=gb2312" %>
response.setContentType("text/html;charset=gb2312");  

//通过 response响应设置返回客户端的页面属性


第二部分:HTML标签

Java代码 复制代码
  1. <html>   
  2. <head>   
  3. <title>Show time</title>   
  4. </head>   
  5. ..   
  6. </html>  
<html>
<head>
<title>Show time</title>
</head>
..
</html>

 

Java代码 复制代码
  1. out.write("\r\n");   
  2. out.write("<html>\r\n");   
  3. out.write("<head>\r\n");   
  4. out.write("<title>Show time</title>\r\n");   
  5. out.write("</head>\r\n");   
  6. out.write("<body> \r\n");   
  7. out.write("\tHello : \r\n");   
  8. out.write("\t");  
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Show time</title>\r\n");
out.write("</head>\r\n");
out.write("<body> \r\n");
out.write("\tHello : \r\n");
out.write("\t");

//通过 out对象 向客户端写HTML标签



第三部分:声明的对象

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



在 _jspService 方法中声明的局部变量

Java代码 复制代码
  1. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");   
  2. String str = format.format(new Date());  
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String str = format.format(new Date());




第四部分:表达式

Java代码 复制代码
  1. <%=str %>   
  2.   
  3. out.print(str ); //写 即打印str变量的值  
<%=str %>

out.print(str ); //写 即打印str变量的值


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics