JSP Life Cycle

The JSP technology is based on page-compilation. Instead of interpreting the JSP page, the web container converts it into a servlet class, and compiles it. This process typically happens when the web container invokes a JSP pages for the first time in response to a request or at container startup. Web container also allow us to precompile JSP pages into servlets.
The First time a JSP page loaded by the JSP container (also called the JSP engine) , the servlet code necessary to implement the JSP tags is automatically generated, compiled , and loaded int the servlet container. This phase is called translation time. These are following step of JSP page life cycle :
  1. Translate the JSP to servlet code.
  2. Comiple the servlet to bytecode.
  3. Load the servlet class.
  4. Create the servlet instance.
  5. Call the jspInit method.
  6. Call the _jspService method.
  7. Call the jspDestroy method.
jsp lifecycle
1.  JSP Page Translation:
A java servlet file is generated from the JSP source file. This is the first step in its the web container translate the JSP file into a Java source code that contains a servlet class definition. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.
jsp lifecycle
2. JSP Page Compilation:
The generated java servlet file is compiled into a java servlet class.
Note: The translation of a JSP source page into its implementation class can happen at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.
jsp lifecycle
3. Class Loading:
The java servlet class bytecode is loaded into the web container's JVM using a classloader.
jsp lifecycle
4. Execution phase:
In the execution phase the container manages one or more instances of this class in response to requests and other events.
The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().
jsp lifecycle
5. Initialization:
jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle.
jsp lifecycle
6. _jspService() execution:
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the jspService method in the JSP. The jspService method takes an HttpServletRequest and an HttpServletResponse as its parameters. It passes the request and the response objects. _jspService() cannot be overridden.
jsp lifecycle
7. jspDestroy() execution:
The jspDestroy method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when we need to perform any cleanup, such as releasing database connections or closing open files. The jspDestroy method is declared this way:
<%! public void jspDestroy() { // Your cleanup code goes here. } %>
jsp lifecycle
 



 

Latest Tutorial


Most Visited Tutorial