19. Explain the life cycle of a JSP.

A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.

  1. JSP page translation
  2. JSP page compilation
  3. load class
  4. create instance
  5. call the jspInit method
  6. call the _jspService method
  7. call the jspDestroy method

             When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.

            The translation involves bringing in any included files, skipping any jsp comments and leaving in any of the HTML comments. The source is then compiled using the same javac compiler that you would use to compile any other Java program. Of course the result of many of the attempts at compilation will be compilation errors that require modifications to the JSP source. If a JSP is configured with <load-on-startup> in the deployment descriptor, the container will attempt to compile when it starts rather than waiting for the first request.

             After the page has been translated and compiled, the JSP page’s servlet (for the most part) follows the servlet life cycle.

  • If an instance of the JSP page’s servlet does not exist, the container
    Loads the JSP page’s servlet class
  • Instantiates an instance of the servlet class
  • Initializes the servlet instance by calling the jspInit method
  • The container invokes the _jspService method, passing request and response objects.
  • If the container needs to remove the JSP page’s servlet, it calls the jspDestroy method.

Initialization: The jspInit method is called by the container once and only once for a servlet instance. As you might guess from its name it is used to do initial once only setup such as getting resources and initialising variables that are used in the JSP page. The jspInit method can be overridden within a JSP page with code like the following.

<%!
public void jspInit(){
/*getServletConfig is inherited */
ServletConfig config= getServletConfig();
}
%>

Execution: The _jspService method is the equivalent of the service() method for a servlet. The _jspService method is called for each request, taking the request and response objects as parameters. The scriptlets, html elements and expressions are put into this method as part of translation. The _jspService method makes the implicit objects available to the JSP page.The _jspService method cannot be overridden within a JSP page. 
Cleanup: The jspDestroy method is guaranteed to to be the last method called by the container before an instance of a JSP page goes out of service. The jspDestroy method is typically used to clean up or release resources acquired by the jspInit method. The jspDestory method is a little like the finalize method in a plain old Java class, in that you cannot be certain exactly when it will run. Because of this be aware that it may not be the most efficient way of cleaning up precious resources.

Advertisements

5 Responses

  1. good

  2. Really nice………..it solves my doubt.

  3. very good and understand

  4. Nice explanation !!

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: