JSP Life Cycle
JSP Life Cycle
Life Cycle is composed of several stages that provides a easy way
to understand an application. JSP life cycle consists of
following stages:
1) JSP Page Translation-During Translation stage, JSP page
is translated into a servlet and it is servlet which is
actually runs when a browser makes a request. When the JSP requests
, it is parsed for syntactical request and converted into Servlet
source file. The translation of JSP source page
into its implementation class i.e. Servlet can occur at anytime
between initial deployment of JSP page. It is done
before Compilation.
2) JSP Page Compilation-During this stage , JSP page may be
compiled into its implementation class. In this way JSP page
authoring tools and JSP Tag Libraries may be used for
authoring Servlets. Compilation of JSP page in the
context of web application provide resolution of related url
specifications. A JSP page can also compiled at deployment time.
3) Load Class-JSP servlet is loaded.
4) Create Instance-JSP servlet instance is created. The
Servlet class corresponding to jsp page must implement the interface
javax.servlet.jsp.HttpJspPage or javax servlet.jsp.JspPage interface
like this:
| package javax.servlet.jsp;
public interface JspPage extends javax.servlet.Servlet { public void _jspService(ServletRequestSubtype request,ServletResponseSubtype throws ServletException, IOException; } |
5)The jspInit() -This method is called by jsp
Container once for a servlet instance. As the name
suggests it is called to prepare the page before the first request is delivered.Method is invoked when the JSP page is initialized. When method is called all
the methods in servlet, including getServletConfig are
available.
| <%!
public void jspInit() { } %> |
6) The jspService()- The jspService() method is
called for each request taking the requests and response
objects as parameters. The Scriplets , html elements and expressions are
put into this method as part of translation.This
method makes implicit objects available to jsp page and cannot
be overridden within a jsp page.It is defined automatically by the JSP
container and invoked at each client request .
| public void _jspService(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res) throws IOException, ServletException { } |
7) The jspDestroy()-The jspDestroy() is the last
method to be called by jsp container before the instance of
jsp page goes out of service. It is used to cleanup and release resources
acquired by jspInit().As such ,it is invoked before destroying the
page.
| <%!
public void jspDestroy() { } %> |
Here is an example which illustrates jsp life cycle.
| <%@page contentType=”text/html” import=”java.util.*” %>
<HTML> <HEAD><TITLE>JSP Life Cycle Page</TITLE></HEAD> <BODY> <%! int number; public void jspInit() { number = 0; } public void jspDestroy() { number = 4; } %> <% out.println(”The number is ” + number + “<br>”); %> </BODY> </HTML> |
Jsp life cycle consists of three main methods jspInit(), jspService(),
jspDestroy(). jspInit() is called to prepare the page before the
first request is delivered .Here variable number is defined. jspInit() is
called and number is initialized to 0.After that jspDestroy() is called
and here number is initialized to 4. No statement is
executed after the jspDestroy() is called. Therfore first statement is
executed.
JSP Resources
Thanks