728x90 AdSpace

Sunday, 19 April 2015

JSP - Life Cycle

The Life Cycle of a JSP Page

The Life cycle of JSP can be defined ad the entire process from its creation to till destruction which is similar to servlet life cycle with an additional step which is required to compile JSP into servlet.
A JSP page services requests as a Servlet. Thus, the life cycle and many of the capabilities of JSP pages are determined by Java servlet technology.
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.
Path in life cycle of JSP
  1. Translation of JSP Page
  2. Complilation of JSP Page
  3. Loads the JSP page’s servlet class
  4. Instantiates an instance of the servlet class
  5. Initializes the servlet instance by calling jspInit method
  6. The container invokes the _jspService method, passing request and response objects
  7. If the container needs to remove the JSP page’s servlet, it calls the jspDestroy method.

JSP Translation and Compilation

During the translation phase each type of data in a JSP page is treated differently. Static data is transformed into code that will emit the data into the response stream. JSP elements are treated as follows:
  1.     Directives are used to control how the web container translates and executes the JSP page.
  2.     Scripting elements are inserted into the JSP page’s servlet class.
  3.     Expression language expressions are passed as parameters to calls to the JSP expression evaluator.
  4.     jsp:[set|get]Property elements are converted into method calls to JavaBeans components.
  5.     jsp:[include|forward]elements are converted into invocations of the Java Servlet API.
  6.     The jsp:plugin element is converted into browser-specific markup for activating an applet.
  7.     Custom tags are converted into calls to the tag handler that implements the custom tag.
In the Application Server, the source for the servlet created from a JSP page named pageName is in this file:
apache-tomcat-7.0.54/work/Catalina/localhost/WAR-NAME/pageName_jsp.java
Both the translation and the compilation phases can yield errors that are observed only when the page is requested for the first time. If an error is encountered during either phase, the server will return JasperException and a message that includes the name of the JSP page and the line where the error occurred.
After the page has been translated and compiled, the JSP page’s servlet  follows the servlet life cycle described in Servlet Life Cycle:
   1.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
    2. 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.
 

JSP Initialization

When container loads a JSP it invokes jspInit() method before servicing any requests.

JSP Execution

Whenever browser request the JSP page (After Page loaded and initialized )container invokes _jspService() method.

JSP Destroy

The jspDestroy() method is the JSP equivalent of the destroy method for servlets.
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JSP - Life Cycle Rating: 5 Reviewed By: Annamalai Thangaraj