jsp本质

Posted minconding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp本质相关的知识,希望对你有一定的参考价值。

jsp本质:在访问时会被转换为java的类。

  1 /*
  2  * Generated by the Jasper component of Apache Tomcat
  3  * Version: Apache Tomcat/7.0.92
  4  * Generated at: 2019-02-25 03:41:55 UTC
  5  * Note: The last modified time of this file was set to
  6  *       the last modified time of the source file after
  7  *       generation to assist with modification tracking.
  8  */
  9 package org.apache.jsp;
 10 
 11 import javax.servlet.*;
 12 import javax.servlet.http.*;
 13 import javax.servlet.jsp.*;
 14 
 15 public final class page_jsp extends org.apache.jasper.runtime.HttpJspBase
 16     implements org.apache.jasper.runtime.JspSourceDependent {
 17 
 18 
 19     String str="全局变量";
 20 
 21 
 22     public void func1(){
 23     System.out.println("全局方法");
 24 }
 25 
 26 
 27     class C{
 28         private int a;
 29         public void f(){
 30             System.out.println("全局类");
 31         }
 32 }
 33 
 34   private static final javax.servlet.jsp.JspFactory _jspxFactory =
 35           javax.servlet.jsp.JspFactory.getDefaultFactory();
 36 
 37   private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
 38 
 39   private volatile javax.el.ExpressionFactory _el_expressionfactory;
 40   private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
 41 
 42   public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
 43     return _jspx_dependants;
 44   }
 45 
 46   public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
 47     if (_el_expressionfactory == null) {
 48       synchronized (this) {
 49         if (_el_expressionfactory == null) {
 50           _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
 51         }
 52       }
 53     }
 54     return _el_expressionfactory;
 55   }
 56 
 57   public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
 58     if (_jsp_instancemanager == null) {
 59       synchronized (this) {
 60         if (_jsp_instancemanager == null) {
 61           _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
 62         }
 63       }
 64     }
 65     return _jsp_instancemanager;
 66   }
 67 
 68   public void _jspInit() {
 69   }
 70 
 71   public void _jspDestroy() {
 72   }
 73 
 74   public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
 75         throws java.io.IOException, javax.servlet.ServletException {
 76 
 77     final javax.servlet.jsp.PageContext pageContext;
 78     javax.servlet.http.HttpSession session = null;
 79     final javax.servlet.ServletContext application;
 80     final javax.servlet.ServletConfig config;
 81     javax.servlet.jsp.JspWriter out = null;
 82     final java.lang.Object page = this;
 83     javax.servlet.jsp.JspWriter _jspx_out = null;
 84     javax.servlet.jsp.PageContext _jspx_page_context = null;
 85 
 86 
 87     try {
 88       response.setContentType("text/html; charset=utf-8");
 89       pageContext = _jspxFactory.getPageContext(this, request, response,
 90                   null, true, 8192, true);
 91       _jspx_page_context = pageContext;
 92       application = pageContext.getServletContext();
 93       config = pageContext.getServletConfig();
 94       session = pageContext.getSession();
 95       out = pageContext.getOut();
 96       _jspx_out = out;
 97 
 98       out.write("
");
 99       out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
");
100       out.write("<html>
");
101       out.write("<head>
");
102       out.write("<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
");
103       out.write(‘
‘);
104       out.write(‘
‘);
105       out.write(‘
‘);
106       out.write(‘
‘);
107       out.write(‘
‘);
108       out.write(‘
‘);
109 
110     int a=1234;
111     String b="java";
112     out.println(a+b+"局部变量");
113 
114       out.write("
");
115       out.write("<title>Insert title here</title>
");
116       out.write("</head>
");
117       out.write("<body>
");
118       out.print(b );
119       out.write("
");
120       out.write("</body>
");
121       out.write("</html>");
122     } catch (java.lang.Throwable t) {
123       if (!(t instanceof javax.servlet.jsp.SkipPageException)){
124         out = _jspx_out;
125         if (out != null && out.getBufferSize() != 0)
126           try {
127             if (response.isCommitted()) {
128               out.flush();
129             } else {
130               out.clearBuffer();
131             }
132           } catch (java.io.IOException e) {}
133         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
134         else throw new ServletException(t);
135       }
136     } finally {
137       _jspxFactory.releasePageContext(_jspx_page_context);
138     }
139   }
140 }

15 public final class page_jsp extends org.apache.jasper.runtime.HttpJspBase
可以知道,将jsp页面转换为了一个类继承自org.apache.jasper.runtime.HttpJspBase
在jsp中定义的全局变量、全局方法、类等都是实际在该类中定义。
74   public void _jspService(
知道,静态代码部分实际会在该函数以java的方式输出。jsp中定义的局部变量会被定义在这里。
该函数就是jsp的实际执行函数。

以上是关于jsp本质的主要内容,如果未能解决你的问题,请参考以下文章

jsp&el&jstl-01

在 JSP 中的 <% %> 代码片段中添加链接

JSP基础

Jsp第六课 Jsp简介和初步使用

day06-jsp

jsp