JSP Life Cycle

  • JSP is a Web server side programming technology based on Servlet technology. The JSP specification is built on the top of  the Servlet API.
  • Any JSP page is translated into a Java Servlet by a JSP engine at runtime so that the JSP life cycle is determined by Servlet technology. 
  • The JSP engine is a JSP specification implementation which comes with web servers that implement JSP.  Tomcat  is one example. 
  • When a request comes to a JSP page, it may come from client browser or come from another Web component such as a Servlet or JSP. 
  • The Web server asks the JSP engine to check whether the JSP page has never been accessed before, or it has been modified since its last access. If this is the case the JSP engine will
    • Parse the JSP document to translate into a Servlet Java file
    • Compile the Servlet Java file into a class file
  • Then the Servlet container loads the Servlet class for execution and sends the results back to the client.

JSP initialization:
The jspInit() method is executed  first after the JSP is loaded. If the JSP developer needs to perform any JSP-specific initialization such as database connections at the beginning this method can be specified. <%!  . . . %> is a JSP declaration element which is used to declare variable or methods. The jspInit() is only called once during  any JSP component life time.
<%! public void jspInit(){ . . . }   %>
JSP execution :
public  _service(HttpServletRequest req, HttpServletResponse res)  
is a JSP service method  the same as the service() method of a Servlet class. This method never needs to be customized. All Java code defined in scripting elements are inserted in this method by JSP engine.

JSP termination:
<%! public void jspDestroy(){ . . . } %>  
This method allows developers to specify resource cleanup jobs such as database disconnections.