JSP Life Cycle - JEE
Home » JEE, Jsp, Tech Notes

JSP Life Cycle

15 April 2009 18 Comments

In previous article we wrote our first jsp. In this article, we will go further and see how a jsp is displayed using web container. When a web container or servlet container receives a request from client for a jsp page, it takes the jsp through its various life cycle phases, and then returns the response to the client. What all things these containers should support, is defined by the jsp and servlet specifications. The web containers can be a part of web servers, e.g. tomcat, and application servers.

Following diagram shows the different life cycle stages of jsp. Broadly, these stages can be classified into three.

  • Instantiation
  • Request Processing
  • Destruction

Jsp Life Cycle

Instantiation:

When a web container receives a jsp request (may be first or subsequent), it checks for the jsp’s servlet instance. If no servlet instance is available or if it is older than the jsp, then, the web container creates the servlet instance using following stages.

  • Translation
  • Compilation
  • Loading
  • Instantiation
  • Initialization

Translation:

Web container translates (converts) the jsp code into a servlet code. This means that jsp is actually a servlet. After this stage, there is no jsp, everything is a servlet. This task will create a complete jsp page, by considering all included components. Here on, the static content and dynamic contents are treated differently. The resultant is a java class instead of an html page (which we wrote). This is how the structure of a jsp compiled into a java class will be.

package org.apache.jsp.WEB_002dINF.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class firstJsp_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
  ............
  ............
  public Object getDependants() {
    return _jspx_dependants;
  }
  public void _jspInit() {
	............
	............
  }
  public void _jspDestroy() {
	............
	............
  }
  public void _jspService(HttpServletRequest request,
    HttpServletResponse response)
        throws java.io.IOException, ServletException {
	............
	............
  }
	............
	............
}

Compilation:

The generated servlet is compiled to validate the syntax. As it is a java class, the compilation is done using javac command. This will generate the byte code to be run on JVM.

Loading:

The compiled byte code is loaded by the class loader used by web container. This is a standard process of using any java class.

Instantiation:

In this step, instance of the servlet class is created so that it can serve the request.

Initialization:

Initialization is done by calling the jspInit() method. This is one time activity at the start of the initialization process. Initialization will make the ServletContext and ServletConfig objects available. One can access many attributes related to the web container and the servlet itself. After initialization the servlet is ready to process requests.

Request Processing:

Entire initialization process is done to make the servlet available in order to process the incoming request. jspService() is the method that actually processes the request. It prints the response in html (any other) format, using ‘out’ object.

Destroy:

Whenever the server is shutting down or when the server needs memory, the server removes the instance of the servlet. The destroy method jspDestroy() can be called by the server after initialization and before or after request processing. Once destroyed the jsp needs to be initialized again.

Just to summarize, web container handles incoming requests to a jsp by converting it into a servlet and then by using this servlet to generate the response. Also when the server shuts down, the container needs to clear the instances.

Controlling Life Cycle:

In above discussion, we understood that the jsp initialization phase happens when the first request hits the web container. This might take more time to present the response to the first user of jsp. It should be possible to keep initialized servlets ready to receive first request and immediately return response. This can be achieved by using the attributes provided by web and application server. The attribute can be “load-on-startup”, “pre compile” etc.

<<Previous   Home   Next>>

 

More Related Posts in JEE, Jsp, Tech Notes

18 Comments »

  • Nanda kumar said:

    Thank you very much…..Defined Very Well….

    Thanks A lot……

  • vikas singla said:

    Thanks a lot. It helped me to understand the concept very clearly.

  • saima zuberi said:

    very nicly explnd well thanks a lot!!!

  • Mustafa said:

    Ok, Grateful to you. That was clear. Enough to be understood …
    Thanks.

  • Vedraj M. said:

    Thankuuu soo much….

  • mokenti said:

    thanks, u made it clear as crystal

  • siva said:

    this is very usefull to us
    thanks

  • Sameer Jauhari said:

    nice way of exaplination but when

    “_jspInit()” all other methods with “_” will be called

  • vasu said:

    thank u..,my doubts r cleared

  • tiji said:

    good explanation .thank u very much.U can add like ” for each request servlet create new thread”.

  • sandeep said:

    info was very apt…..good work…just what i needed :)

  • Sonis said:

    Thanks a lot…

  • Udara said:

    Great explanation… Thanks

  • Viral said:

    nice Explanation ..Use full and fully funda clear

  • Sakthi said:

    Cleared my doubts!!! Nice!!!

  • xxnations said:

    Great Help..

  • sathish kumar said:

    Excellent Explanation…
    i got perfect answer.
    and thanks

  • paul said:

    thnx a lot.
    it’s really the best jsp example i’ve ever seen.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.