Search This Blog

Monday 20 February 2017

JSP Lifecycle

JSP Lifecycle

The lifecycle of a JSP is the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step, which translates the JSP into a Servlet.
Following are the JSP Lifecycle phases.
1.     Translation of JSP to Servlet code.
2.     Compilation of Servlet to bytecode.
3.     Loading Servlet class.
4.     Creating servlet instance.
5.     Initialization by calling jspInit() method
6.     Request Processing by calling _jspService() method

7.     Destroying by calling jspDestroy() method
JSP Lifecycle
JSP Lifecycle

JSP Translation and Compilation
When a user request for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine does the following
1.   Parse the JSP
2.   Transforms the JSP to a Servlet
3.   Compile the Servlet (.java) file to a class file.


JSP Initialization
Before servicing any request the web container loads the JSP and invokes jspInit() method. Initialization is performed only once, just as with servlets init() method. If you need to perform any JSP-specific initialization, you need yo override the jspInit() method.

JSP Service
Once the JSP 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.
The _jspService() method of a JSP is invoked once per request and is responsible for generating the response for that request.

JSP Destruction
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.
The jspDestroy() method is the JSP similar to the destroy method for servlets. We need to Override jspDestroy() method when we need to perform any cleanup operation, such as releasing database connections or closing files which were opened.