Life cycle of a JSP page consists of two phases, translation phase and execution phase. Every JSP is a Servlet, a JSP page is translated and compiled into servlet and the resulting servlet handles the request, So life cycle of a JSP page largely depends on the Servlet API.
JSP engine does the following 7 phases.
• Page translation: page is parsed, and a java file which is a servlet is created.
• Page compilation: page is compiled into a class file
• Page loading : This class file is loaded.
• Create an instance : Instance of servlet is created
• jspInit() method is called
• jspService is called to handle service calls
• jspDestroy is called to destroy it when the servlet is not required.
Translation phase
During the translation phase, JSP page is translated into a servlet class. The entire static markup in the JSP page is converted into the code that writes the data to response stream. If you look at the source of the generated Servlet class you will find calls to the out.write() which write data to ServletOutputStream.
If you have following HTML code into your JSP page
JSP life cycle tutorial
It will generate code like
out.write(“JSP life cycle tutorial”)
During the translation phase JSP elements are treated as follows:
• JSP directives controls the behavior of the resultant servlet.
• Scripting elements results into the equivalent Java code.
• Custom tags are converted into the code that calls method on tag handlers.
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.
Class Loading:
The java servlet class that was compiled from the JSP source is loaded into the container
Execution phase
JSP life cycle’s execution phase is almost similar to that of the Servlet life cycle, because ultimately it’s a servlet which is being executed. The Servlet class generated at the end of the translation phase represents the contract between container and the JSP page. According to the JSP specification the servlet class must implement the HttpJspPage interface which defines the life cycle methods.
JSP life cycle includes three methods jspInit(), _jspService() and jspDestroy()
Initialization:
jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle.
_jspService() execution:
This method is called for every request of this JSP during its life cycle. This is where it serves the purpose of creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the response objects. _jspService() cannot be overridden.
jspDestroy() execution:
This method is called when this JSP is destroyed. With this call the servlet serves its purpose and submits itself to heaven (garbage collection). This is the end of jsp life cycle.
jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.
The HttpJspPage Interface
The javax.servlet.jsp.HttpJspPage contains three methods
Public void jspInit()
This method is invoked when the JSP page is initialized. This method is similar to init() method in servlet. If you want to provide initialization for a JSP page, you define this method in declaration part of the JSP page. But most of the time you will not need to define this method.
public void _jspService
void _jspService(HttpServletRequest request, HttpServletResponse response)ThrowsIOException, ServletException
This method represents the body of the JSP page and invoked at each client request. This method is similar to service() method in servlet.
Note: You should never provide implementation _jspService() method as web container automatically generates this method based on the content of the JSP page
Public void jspDestroy()
This method is invoked just before the JSP page is destroyed by the web container. This method is similar to destroy() method in servlet. If you want to provide some cleanup code, you can define this method in declaration part of the JSP page.
Understanding the Servlet life cycle with an example
This example explains how to execute code at JSP initialization phase and destroy phase of JSP Life Cycle.
It is a simple request counter that displays how many time the page has been called or how many requests the page has received. The page declares a counter variable of type integer, the counter is set to zero when the page is initialized. Each time the page receives a request, the value of counter variable is incremented by one and displayed to user. During each life cycle stage, A message is logged to server log.
lifecycle.jsp
<%! int counter; public void jspInit() { counter = 0; log("The lifecycle jsp has been initialized"); } %> <html> <head> <title>JSP Life Cycle Example</title> </head> <body> <% log("The lifecycle jsp has received a request"); counter++; %> <h2>JSP Life cycle : Request counter</h2> <p>This page has been called <%=counter %> times </p> </body> </html> <%! public void jspDestroy() { log("The lifecycle jsp is being destroyed"); } %>