JSP的本质是什么?
概述
- JSP页面在本质上就是Servlet程序,当JSP页面在首次被访问时,Web容器就会将JSP页面转化为Servlet,只需要做一次。如index.jsp在首次被访问时,Web容器会将其翻译成一个index_jsp.java文件,即Servlet代码;
- Servlet程序要被WEB容器调用执行,必须在web.xml中注册映射;
- Servlet类继承了类org.apache.jasper.runtime.HttpJspBase(Tomcat API,Tomcat容器实现的),这个类实现了HttpJspPage, JspPage, Servlet, ServletConfig等这些接口,因此WEB容器必须实现这些接口。
WEB容器: Tomcat、Jetty、Apache、Nginx、Jboss、Undertow、Websphere、Kangle、Resin等。
二、重要API
Servlet API:https://tomcat.apache.org/tomcat-8.5-doc/servletapi/index.html
Tomcat API:https://tomcat.apache.org/tomcat-8.5-doc/api/index.html
JSP API:http://tomcat.apache.org/tomcat-8.5-doc/jspapi/index.html
三、jsp页面转化为servlet程序
(1)index.jsp:
<html>
<body>
<h2>
<% out.println("Hello Tomcat");
%>
</h2>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
(2)index.jsp在首次被访问时,WEB容器将jsp转化为Servlet类,即index.jsp转化为index_jsp.java:
index_jsp.java:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.5.47
* Generated at: 2019-11-07 08:52:49 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants;
} public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages;
} public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes;
} public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager;
} public void _jspInit() {
} public void _jspDestroy() {
} public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final java.lang.String _jspx_method = request.getMethod(); if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html"); 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("<html>\n"); out.write("<body>\n"); out.write("<h2>\n"); out.println("Hello Tomcat"); out.write("\n"); out.write("</h2>\n"); out.write("</body>\n"); out.write("</html>\n");
} catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
从转化后的代码,可知jsp页面里的HTML、CSS、JS,最终都被嵌套在Servlet程序的java代码里。我们可以推断jsp页面只是方便我们编写Servlet程序,就像在拼接一个长长的字符串。但是在前后端分离的开发中(可以参考《前后端分离的趋势》),jsp显然是不适应的。
四、分析index_jsp.java类
- 注释部分
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.5.47
* ...
*/
- 1
- 2
- 3
- 4
- 5
注释里说*.jsp文件都会被Apache Tomcat容器的Jasper组件转化为Servlet的子类。再次证明,jsp程序的本质就是Servlet程序。
- Servlet类的继承关系
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports {
- 1
- 2
- 3
HttpJspBase实现了Serializable, HttpJspPage, JspPage, Servlet, ServletConfig这些接口。而HttpJspBase是Tomcat API,所以Tomcat容器实现了这些。
- 内置对象out、request、response、session、application
我们分析index_jsp.java的_jspService方法来学习这些内置对象。
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final java.lang.String _jspx_method = request.getMethod(); if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html"); 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;
…
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
(1)request
request是_jspService方法的参数,类型为HttpServletRequest,专门用来获得客户端向服务端请求的信息。
(2)reponse
reponse是_jspService方法的参数,类型为HttpServletResponse,专门用来填充要发送给客户端的数据。
(3)out、session、application
_jspxFactory对象调用 getPageContext()方法返回 PageContext对象。再通过调用PageContext对象的相应方法来初始out、session、application这三个内置对象。
private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory();
- 1
- 2
从上面的代码可知:
out是一个 JspWriter对象。JspWriter对象与ServletResponse的PrintWriter关联着,所以你会发现不用直接使用reponse对象,也可以往response对象写数据。
JspWriter的方法请参考:http://tomcat.apache.org/tomcat-8.5-doc/jspapi/index.html
session是一个 HttpSession对象。用来维护会话期间用户的信息。关闭浏览器此会话就会被关闭。
HttpSession的方法请参考:https://tomcat.apache.org/tomcat-8.5-doc/servletapi/index.html
application是一个 ServletContext对象。它的生命周期与程序一样长,只有程序被关闭,它才会消亡。
ServletContext的方法请参考:https://tomcat.apache.org/tomcat-8.5-doc/servletapi/index.html
小知识:HTML、CSS、JS的处理是由客户端的浏览器来完成的,WEB服务器只需要将这些资源返回给浏览器即可,动态部分都是组装好再发送的。
谢谢阅读!
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/103041960
- 点赞
- 收藏
- 关注作者
评论(0)