1、編譯階段:
servlet容器編譯servlet源文件,生成servlet類。
觀察一個JSP頁面在第一次訪問的時候會由servlet容器會生成.java文件,最終編譯成.class字節碼文件,如果打開.java文件查看,就是一個servlet。
2、初始化階段:
加載與JSP對應的servlet類,創建其實例,並調用它的初始化方法。
3、執行階段:
調用與JSP對應的servlet實例的服務方法。
4、銷毀階段:
調用與JSP對應的servlet實例的銷毀方法,然后銷毀servlet實例。
如下圖所示的是servlet的生命周期和JSP非常的相似:
下面來測試一下JSP實現這些生命周期,在left.jsp頁面如下寫法:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>life.jsp</title> </head> <body> <%! private int initVar=0; private int serviceVar=0; private int destroyVar=0; %> <%! public void jspInit(){ initVar++; System.out.println("jspInit(): JSP被初始化了"+initVar+"次"); } public void jspDestroy(){ destroyVar++; System.out.println("jspDestroy(): JSP被銷毀了"+destroyVar+"次"); } %> <% serviceVar++; System.out.println("_jspService(): JSP共響應了"+serviceVar+"次請求"); String content1="初始化次數 : "+initVar; String content2="響應客戶請求次數 : "+serviceVar; String content3="銷毀次數 : "+destroyVar; %> <p><%=content1 %></p> <p><%=content2 %></p> <p><%=content3 %></p> </body> </html>
效果如下:
然后打開工作空間的生成的java文件:“\workspace_web_test\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\webtest1\org\apache\jsp\life_jsp.java”
會發現如下結構:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.68 * Generated at: 2016-12-14 13:18:33 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 life_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private int initVar=0; private int serviceVar=0; private int destroyVar=0; public void jspInit(){ initVar++; System.out.println("jspInit(): JSP被初始化了"+initVar+"次"); } public void jspDestroy(){ destroyVar++; System.out.println("jspDestroy(): JSP被銷毀了"+destroyVar+"次"); } 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 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 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 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("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<title>life.jsp</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.write("\r\n"); out.write("\r\n"); out.write(" \r\n"); out.write("\r\n"); out.write("\r\n"); serviceVar++; System.out.println("_jspService(): JSP共響應了"+serviceVar+"次請求"); String content1="初始化次數 : "+initVar; String content2="響應客戶請求次數 : "+serviceVar; String content3="銷毀次數 : "+destroyVar; out.write("\r\n"); out.write("<p>"); out.print(content1 ); out.write("</p>\r\n"); out.write("<p>"); out.print(content2 ); out.write("</p>\r\n"); out.write("<p>"); out.print(content3 ); out.write("</p>\r\n"); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } 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); } } }
參考: