JSP 原理

学习笔记,转自

http://sunfish.iteye.com/blog/1492052

http://java-mans.iteye.com/blog/1642280

http://chenhua-1984.iteye.com/blog/763927

 

 

一、JSP原理介绍

 

jsp是运行在web服务端的JAVA展现技术,JSP的实质是一个servlet,也就是一个java文件,在运行的时候也要被JVM编译和装载

 




 二、Jsp运行原理

 

在一个JSP文件第一次被请求时,JSP引擎把该JSP文件转换成为一个Servlet。而这个引擎本身也是一个Servlet。JSP的运行过程如下所示:
(1)JSP引擎先把该JSP文件转换成一个Java源文件(Servlet),在转换时如果发现JSP文件有任何语法错误,转换过程将中断,并向服务端和客户端输出出错信息。
(2)如果转换成功,JSP引擎用javac把该Java源文件编译成相应的class文件。
(3)创建一个该Servlet(JSP页面的转换结果)的实例,该Servlet的jspInit()方法被执行,jspInit()方法在Servlet的生命周期中只被执行一次。
(4)jspService()方法被调用来处理客户端的请求。对每一个请求,JSP引擎创建一个新的线程来处理该请求。如果有多个客户端同时请求该JSP文件,则JSP引擎会创建多个线程。每个客户端请求对应一个线程。以多线程方式执行可以大大降低对系统的资源需求,提高系统的并发量及响应时间。但不过也应该注意多线程的编程限制,由于该Servlet始终驻于内存,所以响应是非常快的。
(5)如果.jsp文件被修改了,服务器将根据设置决定是否对该文件重新编译,如果需要重新编译,则将编译结果取代内存中的Servlet,并继续上述处理过程。
(6)虽然JSP效率很高,但在第一次调用时由于需要转换和编译而有一些轻微的延 迟。此外,在任何时候如果由于系统资源不足的原因,JSP引擎将以某种不确定的方式将Servlet从内存中移去。当这种情况发生时jspDestroy()方法首先被调用。
(7)然后Servlet实例便被标记加入“垃圾收集”处理。可在jspInit()中进行一些初始化工作,如建立与数据库的连接,或建立网络连接,从配置文件中取一些参数等,在jspDestory()中释放相应的资源。

 

三、jsp原理图


 

 

其步骤如下:
    1.jsp引擎将jsp文件翻译为Servlet源程序;
    2.之后又将Servlet源程序编译为.class类文件;
其中:
     1.Jsp引擎是通常一个Servlet程序,Tomcat中的jsp引擎就是org.apache.jasper.servlet.JspServlet;
     2.在Tomcat中编译的源文件和.class文件放在”[TOMCAT_HOME]\work\Catalina\[主机名,如localhost]\应用程序名称” 目录下;
     3.默认情况下,第一次访问某jsp文件时,才对该jsp文件进行翻译、编译,所以较慢。不过在以后的访问中将不会在出现该情况,因为已经被编译过了,呵呵。

 

 

四、jsp相关类

 

4.1 jsp相关uml图

 

 

   其中,org.apache.jsp.index_jsp是tomcat将index.jsp转化为servlet源代码后的结果。其代码可以在如下目录中看到(当然,你要首先访问index.jsp才会翻译,编译)

 

 

4.2 具体类源代码

 

 

4.2.1 JspPage源代码

 

 

Java代码 
  1. package javax.servlet.jsp;  
  2.   
  3. import javax.servlet.Servlet;  
  4.   
  5. public abstract interface JspPage extends Servlet  
  6. {  
  7.   public abstract void jspInit();  
  8.   
  9.   public abstract void jspDestroy();  
  10. }  

 

4.2.2 HttpJspPage源代码

 

 

Java代码 
  1. package javax.servlet.jsp;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.   
  8. public abstract interface HttpJspPage extends JspPage  
  9. {  
  10.   public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)  
  11.     throws ServletException, IOException;  
  12. }  

 

 

4.2.2 HttpJspBase 源代码

 

    和HttpJspPage名称虽然很香精 ,但已经位于不同的包中了。下面的源代码是我从tomcat6中的jasper.jar拉出来的。

 

 

Java代码 
  1. package org.apache.jasper.runtime;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.ServletConfig;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import javax.servlet.jsp.HttpJspPage;  
  10. import org.apache.jasper.compiler.Localizer;  
  11.   
  12. public abstract class HttpJspBase extends HttpServlet  
  13.   implements HttpJspPage  
  14. {  
  15.   public final void init(ServletConfig config)  
  16.     throws ServletException  
  17.   {  
  18.     super.init(config);  
  19.     jspInit();  
  20.     _jspInit();  
  21.   }  
  22.   
  23.   public String getServletInfo() {  
  24.     return Localizer.getMessage("jsp.engine.info");  
  25.   }  
  26.   
  27.   public final void destroy() {  
  28.     jspDestroy();  
  29.     _jspDestroy();  
  30.   }  
  31.   
  32.   public final void service(HttpServletRequest request, HttpServletResponse response)  
  33.     throws ServletException, IOException  
  34.   {  
  35.     _jspService(request, response);  
  36.   }  
  37.   
  38.   public void jspInit()  
  39.   {  
  40.   }  
  41.   
  42.   public void _jspInit()  
  43.   {  
  44.   }  
  45.   
  46.   public void jspDestroy()  
  47.   {  
  48.   }  
  49.   
  50.   protected void _jspDestroy()  
  51.   {  
  52.   }  
  53.   
  54.   public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)  
  55.     throws ServletException, IOException;  
  56. }  

 

 

4.2.2 JspSourceDependent源代码

 

 

Java代码 
  1. package org.apache.jasper.runtime;  
  2.   
  3. public abstract interface JspSourceDependent  
  4. {  
  5.   public abstract Object getDependants();  
  6. }  

 

 

4.2.2  org.apache.jsp.index_jsp 源代码

 

       上面提到过org.apache.jsp.index_jsp是tomcat将index.jsp转化为servlet的源代码。

 

 

 

Java代码 
  1. package org.apache.jsp;  
  2.   
  3. import javax.servlet.*;  
  4. import javax.servlet.http.*;  
  5. import javax.servlet.jsp.*;  
  6. import java.util.*;  
  7.   
  8. public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase  
  9.     implements org.apache.jasper.runtime.JspSourceDependent {  
  10.   
  11.   private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  
  12.   
  13.   private static java.util.List _jspx_dependants;  
  14.   
  15.   private javax.el.ExpressionFactory _el_expressionfactory;  
  16.   private org.apache.AnnotationProcessor _jsp_annotationprocessor;  
  17.   
  18.   public Object getDependants() {  
  19.     return _jspx_dependants;  
  20.   }  
  21.   
  22.   public void _jspInit() {  
  23.     _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();  
  24.     _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());  
  25.   }  
  26.   
  27.   public void _jspDestroy() {  
  28.   }  
  29.   
  30.   public void _jspService(HttpServletRequest request, HttpServletResponse response)  
  31.         throws java.io.IOException, ServletException {  
  32.   
  33.     PageContext pageContext = null;  
  34.     HttpSession session = null;  
  35.     ServletContext application = null;  
  36.     ServletConfig config = null;  
  37.     JspWriter out = null;  
  38.     Object page = this;  
  39.     JspWriter _jspx_out = null;  
  40.     PageContext _jspx_page_context = null;  
  41.   
  42.   
  43.     try {  
  44.       response.setContentType("text/html;charset=GB18030");  
  45.       pageContext = _jspxFactory.getPageContext(this, request, response,  
  46.                 nulltrue8192true);  
  47.       _jspx_page_context = pageContext;  
  48.       application = pageContext.getServletContext();  
  49.       config = pageContext.getServletConfig();  
  50.       session = pageContext.getSession();  
  51.       out = pageContext.getOut();  
  52.       _jspx_out = out;  
  53.   
  54.       out.write('\r');  
  55.       out.write('\n');  
  56.   
  57. String path = request.getContextPath();  
  58. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  59.   
  60.       out.write("\r\n");  
  61.       out.write("\r\n");  
  62.       out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");  
  63.       out.write("<html>\r\n");  
  64.       out.write("  <head>\r\n");  
  65.       out.write("    <base href=\"");  
  66.       out.print(basePath);  
  67.       out.write("\">\r\n");  
  68.       out.write("    \r\n");  
  69.       out.write("    <title>My JSP 'index.jsp' starting page</title>\r\n");  
  70.       out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");  
  71.       out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");  
  72.       out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");  
  73.       out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");  
  74.       out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");  
  75.       out.write("\t<!--\r\n");  
  76.       out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");  
  77.       out.write("\t-->\r\n");  
  78.       out.write("  </head>\r\n");  
  79.       out.write("  \r\n");  
  80.       out.write("  <body>\r\n");  
  81.       out.write("    This is my JSP page. <br>\r\n");  
  82.       out.write("  </body>\r\n");  
  83.       out.write("</html>\r\n");  
  84.     } catch (Throwable t) {  
  85.       if (!(t instanceof SkipPageException)){  
  86.         out = _jspx_out;  
  87.         if (out != null && out.getBufferSize() != 0)  
  88.           try { out.clearBuffer(); } catch (java.io.IOException e) {}  
  89.         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);  
  90.       }  
  91.     } finally {  
  92.       _jspxFactory.releasePageContext(_jspx_page_context);  
  93.     }  
  94.   }  
  95. }  

 

其对应的index.jsp为

 

 

Html代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     This is my JSP page. <br>  
  25.   </body>  
  26. </html>  

 

4.2.3 九大隐藏对象

 

    从jsp转换的源码中,很容易的就可以发现了传说中的9大jsp内置隐藏对象中的8个。如下:

 

 

Java代码 
  1. PageContext pageContext = null;  
  2. HttpSession session = null;  
  3. ServletContext application = null;  
  4. ServletConfig config = null;  
  5. JspWriter out = null;  
  6. Object page = this;  
  7. JspWriter _jspx_out = null;  
  8. PageContext _jspx_page_context = null;  

 

    至于第9个,只有page指令被设置为:

 

 

Html代码 
  1. <%@ page isErrorPage="true" %>  

 

    才会出现:

 

 

Java代码 
  1. Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值