Books Related to Java, Core Java, Jquery, Web Services, SCJP, Spring, Hibernate and J2ME

Friday, January 23, 2009

Life Cycle of JSP

JSP technology enables us to create web based application. It sends its request as a Servlet. Therefore, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.

A Servlet handles the request that is mapped to a JSP page and checks whether the JSP page's servlet is older than the JSP page. Then it translates the JSP page into a servlet class and compiles the class.

JSP Life Cycle has three important methods that is:

  1. init():- This method is called when the instance is created and it is called only once during JSP life cycle. It is called for the Servlet instance initialization.
  2. service:- This method is called for every request of this JSP during its life cycle. It passes the request and the response objects. jspService() cannot be overridden.
  3. destroy():- This method is called when this JSP is destroyed and will not be available for future requests.

Phases of JSP Life Cycle:

  1. Translation:
    The JSP source file generates a java servlet file. Generated servlet implements the interface javax.servlet.jsp.HttpJspPage. The interface HttpJspPage extends the interface JspPage. This interface JspPage extends the interface javax.servlet.Servlet.
  2. Compilation:
    The generated java servlet file is compiled into a java servlet class. The generated servlet class thus implements all the methods of the above said three (javax.servlet.jsp.HttpJspPage, JspPage, javax.servlet.Servlet) interfaces.
  3. Class Loading:
    The java servlet class that was compiled from the JSP source is loaded into the container.
  4. Instance Creation:
    An instance is created for the loaded servlet class. The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().

No comments:

Post a Comment