Saturday, April 28, 2012

Understanding JSP Life cycle

This tutorial explains JSP life cycle. You will get better understanding of how JSP pages are initialized, invoked and destroyed. If you are already familiar with Servlet life cycle, than understanding the JSP life cycle would become easier. You can read more about servlet life cycle here
Life cycle of a JSP page consists of two phases, translation phase and execution phase. As I said earlier in ‘section 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 Life Cycle : 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
 <b>JSP life cycle tutorial<b>
It will generate code like
out.write(“<b>JSP life cycle tutorial<b>”)
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 Life Cycle : 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()

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) Throws IOException, 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.

A JSP Life Cycle example : Request counter

The following code snippet explains the JSP Life Cycle. 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");
 }
 %>

How to run the example

To run this simple JSP page, you can just create a directory with name 'lifecycle' under 'webapps' directory of tomcat server and copy the lifecycle.jsp there. After starting the server, you should be able to access it at url http://localhost:8080/lifecycle/lifecycle.jsp
Log messages can be found inside log file with todays date in 'logs' directory of tomcat server.
Note: It is not a good practice to have jspInit and jspDestroy methods in your JSP page. Infact it is considered a bad practice to have any java code inside JSP. I just explained it for the sake of understanding. If you ever need to have initialization or destroy code in your JSP page, most probably you need a servlet and not the JSP.

No comments:

Post a Comment