October 11, 2012

Understanding JSP Life Cycle

JSP never outputs the content directly to the browser. Instead, JSP relies on initial server-side processing process which translates JSP page into the JSP Page class. The page class then will handle all the requests made of JSP. The JSP life cycle is described as the picture below:


JSP life cycle can be divided into four phases: Translation, Initialization, execution and finalization.

Translation

In the translation phase, JSP engine checks for the JSP syntax and translates JSP page into its page implementation class if the syntax is correct. This class is actually a standard Java servlet. After that, JSP engine compiles the source file into class file that is ready to use.
If the container receives the request, it checks for the changes of the JSP page since it was last translated. If no changes was made, It just loads the servlet otherwise the process of check, translate and compile takes place again. Because the compilation process takes time so JSP engine wants to minimize it to increase the performance of the page processing.

Initialization

After the translation phase, JSP engine loads the class file and create an instance of of the servlet to handle processing of the initial request. JSP engines will call a method jspInit() to initialize the a servlet. jspInit method is generated during the translation phase which is normally used for initializing application-level parameters and resources. You can also overide this method by using declaration.

<%!
   public void jspInit(){
      // put your custom code here  
   }
%>

Execution

After the initialization phase, the web container calls the method _jspService() to handle the request and returning a response to the client. Each request is handled is a separated thread. Be noted that all the scriptlets and expressions end up inside this method. The JSP directives and declaration are applied to the entire page so the are outside of this method.

Finalization

In the finalization phase, the web container calls the method jspDestroy(). This method is used to clean up memory and resources. Like jspInit() method, you can override thejspDestroy() method also to do your all clean up such as release the resources you loaded in the initialization phase....
<%!
   public void jspDestroy(){
      // put your custom code here 
      // to clean up resources
   }
%>

4 comments:

  1. In the JSP lifecycle, the destroy method is also invoked when the server shuts down

    ReplyDelete
  2. I have also started creating blog on java. Its in still new phase.
    You are invited to see my blog and put your valuable comments
    http://javacodeimpl.blogspot.com

    ReplyDelete
  3. This is the first time I've been to your website.

    Thank you for posting more information.

    Feel free to surf to my web blog ... tutor In Melbourne

    ReplyDelete
  4. Perfect explanation.. Thanks.. Please explain about Struts Life cycle

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts