JSP 02: 执行过程
为什么JSP可以在html中运行java代码? 这是 步骤 1 :
步骤1: 执行过程
因为JSP被转译成了Servlet
1. 把 hello.jsp转译为hello_jsp.java
2. hello_jsp.java 位于
d:\tomcat\work\Catalina\localhost\_\org\apache\jsp
3. hello_jsp.java是一个servlet
4. 把hello_jsp.java 编译为hello_jsp.class
5. 执行hello_jsp,生成html
6. 通过http协议把html 响应返回给浏览器
步骤 2 : hello_jsp.java 为什么是Servlet
在Servlet章节中,我们说HelloServlet是一个Servlet,不是因为它的类名里有一个"Servlet",而是因为它继承了 HttpServlet
打开转译hello.jsp 后得到的hello_jsp.java,可以发现它继承了类
(D:\tomcat\work\Catalina\localhost\_\org\apache\jsp\hello_jsp.java)
org.apache.jasper.runtime.HttpJspBase
而HttpJspBase 继承了HttpServlet
所以我们说hello_.jsp.java 是一个Servlet
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.64 * Generated at: 2017-01-07 01:45:17 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.*; import java.util.*; public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { 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 javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } 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 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("你好 JSP\r\n"); out.write(" \r\n"); out.write("<br>\r\n"); out.write(" \r\n"); out.print(new Date().toLocaleString()); } 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); } } }