本帖最后由 阿莱 于 2019-1-22 20:49 编辑
前言:
我们知道JSP可以编写html代码,也可以编写java代码,那么这些html的内容是如何展示在浏览器上的?java代码是如何被执行的?带着这些问题我们一起来看下JSP的运行原理,了解了运行原理这些问题将会迎刃而解。
准备工作:
我们在开发工具IDEA中新建一个web项目名称为:jsp_demo,在web目录写创建hello.jsp 代码内容如下:
[HTML] 纯文本查看 复制代码 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
<%--html代码--%>
<h1>Hello Jsp</h1>
System.out.println("Hello World");
<%--java代码--%>
<%
System.out.println("Hello Java.......");
request.setAttribute("name","刘亦菲");
%>
</body>
</html>
启动tomcat服务器进行访问
运行流程分析:
1.浏览器发起请求,向服务器获取hello.jsp
http://localhost:8080/hello.jsp
2.会去tomcat的conf目录下的web.xml中寻找对应的映射路径
3.根绝后缀名匹配的原则,找到<servlet-name>为jsp的JspServlet
4.JspServlet会将hello.jsp翻译为hello_jsp.java,同事便以为hello_jsp.class
该文件在IDEA发布项目的虚拟路径的work目录下:
5.翻译后的hello_jsp.java,继承了抽象类HttpJspBase
6.HttpJspBase继承了抽象类HttpServlet
注意:HttpJspBase在tomcat的lib目录下的jasper.jar中,解压后通过IDEA的反编译可以看到继承关系
此时会执行HttpServlet的service方法,通过查看源码我们发现service方法调用的是_jspSerice方法
[Java] 纯文本查看 复制代码 public final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this._jspService(request, response);
}
由于hello_jsp.java继承了抽象类HttpJspBase,HttpJspBase又继承了抽象类HttpServlet,因此最终会执行子类的_jspSerice方法:
[Java] 纯文本查看 复制代码 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;charset=UTF-8");
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("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>hello</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write(" ");
out.write("\r\n");
out.write(" <h1>Hello Jsp</h1>\r\n");
out.write(" System.out.println(\"Hello World\");\r\n");
out.write("\r\n");
out.write(" ");
out.write("\r\n");
out.write(" ");
System.out.println("Hello Java.......");
request.setAttribute("name","刘亦菲");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>\r\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);
}
}
我们发现hello_jsp.java中将html的内容以response输出流的方式响应给浏览器,java代码正常的执行。
结论总结:
1.JSP就是Servlet,被编译后的java文件继承了HttpServlet遵循了Servlet的规范
2.JSP的执行时通过tomcat现将JSP文件翻译为Servlet在执行
|