当前位置 : 主页 > 编程语言 > java >

jsp原理

来源:互联网 收集:自由互联 发布时间:2021-06-25
1jsp的本质是Servlet 1.1在idea中启动tomcat: 1.1.1新建子模块项目: 1.1.2编写jsp代码: 1 %-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2019/6/15 0015 5 Time: 21:53 6 To change this template use File | Setti

1jsp的本质是Servlet

1.1在idea中启动tomcat:

1.1.1新建子模块项目:

 

1.1.2编写jsp代码:

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2019/6/15 0015
 5   Time: 21:53
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10   <head>
11     <title>$Title$</title>
12   </head>
13   <body>
14       <%
15         int a=10;
16         String s="hello";
17       %>
18 
19       <div>a=<%=a%></div>
20       <div>s=<%=s%></div>
21 
22       <%!
23         double d=5.21;
24       %>
25       <div>d=<%=d%></div>
26   </body>
27 </html>
View Code

 

1.1.3找到index.jsp生成的.java文件

 

(index_jsp.java)文件代码:

  1 /*
  2  * Generated by the Jasper component of Apache Tomcat
  3  * Version: Apache Tomcat/8.5.32
  4  * Generated at: 2019-06-16 02:07:24 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 index_jsp extends org.apache.jasper.runtime.HttpJspBase
 16     implements org.apache.jasper.runtime.JspSourceDependent,
 17                  org.apache.jasper.runtime.JspSourceImports {
 18 
 19 
 20         double d=5.21;
 21       
 22   private static final javax.servlet.jsp.JspFactory _jspxFactory =
 23           javax.servlet.jsp.JspFactory.getDefaultFactory();
 24 
 25   private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
 26 
 27   private static final java.util.Set<java.lang.String> _jspx_imports_packages;
 28 
 29   private static final java.util.Set<java.lang.String> _jspx_imports_classes;
 30 
 31   static {
 32     _jspx_imports_packages = new java.util.HashSet<>();
 33     _jspx_imports_packages.add("javax.servlet");
 34     _jspx_imports_packages.add("javax.servlet.http");
 35     _jspx_imports_packages.add("javax.servlet.jsp");
 36     _jspx_imports_classes = null;
 37   }
 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 java.util.Set<java.lang.String> getPackageImports() {
 47     return _jspx_imports_packages;
 48   }
 49 
 50   public java.util.Set<java.lang.String> getClassImports() {
 51     return _jspx_imports_classes;
 52   }
 53 
 54   public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
 55     if (_el_expressionfactory == null) {
 56       synchronized (this) {
 57         if (_el_expressionfactory == null) {
 58           _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
 59         }
 60       }
 61     }
 62     return _el_expressionfactory;
 63   }
 64 
 65   public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
 66     if (_jsp_instancemanager == null) {
 67       synchronized (this) {
 68         if (_jsp_instancemanager == null) {
 69           _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
 70         }
 71       }
 72     }
 73     return _jsp_instancemanager;
 74   }
 75 
 76   public void _jspInit() {
 77   }
 78 
 79   public void _jspDestroy() {
 80   }
 81 
 82   public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
 83       throws java.io.IOException, javax.servlet.ServletException {
 84 
 85     final java.lang.String _jspx_method = request.getMethod();
 86     if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
 87       response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
 88       return;
 89     }
 90 
 91     final javax.servlet.jsp.PageContext pageContext;
 92     javax.servlet.http.HttpSession session = null;
 93     final javax.servlet.ServletContext application;
 94     final javax.servlet.ServletConfig config;
 95     javax.servlet.jsp.JspWriter out = null;
 96     final java.lang.Object page = this;
 97     javax.servlet.jsp.JspWriter _jspx_out = null;
 98     javax.servlet.jsp.PageContext _jspx_page_context = null;
 99 
100 
101     try {
102       response.setContentType("text/html;charset=UTF-8");
103       pageContext = _jspxFactory.getPageContext(this, request, response,
104                   null, true, 8192, true);
105       _jspx_page_context = pageContext;
106       application = pageContext.getServletContext();
107       config = pageContext.getServletConfig();
108       session = pageContext.getSession();
109       out = pageContext.getOut();
110       _jspx_out = out;
111 
112       out.write("\n");
113       out.write("\n");
114       out.write("<html>\n");
115       out.write("  <head>\n");
116       out.write("    <title>$Title$</title>\n");
117       out.write("  </head>\n");
118       out.write("  <body>\n");
119       out.write("      ");
120 
121         int a=10;
122         String s="hello";
123       
124       out.write("\n");
125       out.write("\n");
126       out.write("      <div>a=");
127       out.print(a);
128       out.write("</div>\n");
129       out.write("      <div>s=");
130       out.print(s);
131       out.write("</div>\n");
132       out.write("\n");
133       out.write("      ");
134       out.write("\n");
135       out.write("      <div>d=");
136       out.print(d);
137       out.write("</div>\n");
138       out.write("  </body>\n");
139       out.write("</html>\n");
140     } catch (java.lang.Throwable t) {
141       if (!(t instanceof javax.servlet.jsp.SkipPageException)){
142         out = _jspx_out;
143         if (out != null && out.getBufferSize() != 0)
144           try {
145             if (response.isCommitted()) {
146               out.flush();
147             } else {
148               out.clearBuffer();
149             }
150           } catch (java.io.IOException e) {}
151         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
152         else throw new ServletException(t);
153       }
154     } finally {
155       _jspxFactory.releasePageContext(_jspx_page_context);
156     }
157   }
158 }
View Code

 

1.1.4代码解析

 

只简单说明下,一点代码。div标签的内容会被解析如下代码:(红色框)

网友评论