In the core of the JSP, Java Servlet technology is executed therefore, JSP life cycle is likely to Java Servlet technology.
JSP Life Cycle
In this section we will discuss about life cycle of JSP.
Like the each Java based web technology JSP also follows a life cycle for the
execution of an application. Life cycle of any technology expresses that the
execution of any application should be passed through the specified phases. In
the core of the JSP, Java Servlet technology is executed therefore, JSP life
cycle is likely to Java Servlet technology. A Servlet container controls the
following processes when a request is mapped to a Servlet :
Loading of Servlet class.
Instantiation of Servlet class.
Calling of init() method to initialize a Servlet instance.
Calling of service method by passing the request and response objects.
But, what for a request mapped to a JSP page. It is quite different of
request mapping to a Servlet and request mapping to a JSP page. As we have
discussed above a Servlet container controls the processes when a request is
mapped to a Servlet but, when a request is mapped to a JSP page a special
Servlet controls as, at first it checks the version of Servlet and JSP that
whether the Servlet of JSP page is older than the JSP page or not. If it is
older then the JSP page is translated into the Servlet class and then it is
compiled. In JSP build process is performed automatically whereas, in Servlet it
is not performed automatically. Life Cycle of JSP helps in understand its
low-level functionality. JSP life cycle explains the process from its creation
to destruction. As we have discussed already, the life cycle of JSP is likely to
Servlet life cycle are described as follows :
Translation of JSP page : This is the first step of JSP
life cycle. This process is started when a request is mapped to a JSP page
and and special Servlet checks whether the Servlet of JSP page is older than
or not. If it is older then it translates the JSP page into Servlet class.
In this phase container manages the translation of JSP pages and tag files,
interprets the standard directives and actions, checks for the syntaxes and
many more operations. Directives on the JSP page specifies how, the
translation and execution of JSP pages would be performed by the web
container, Scriptlet specifies that the element written inside it would be
inserted into JSP pages servlet class, and the action tags <jsp:XXX.../> on
form specifies the calling of Java Servlet API. An exception ParseException
may be generated at this phase by a server when a JSP page is requested for
the first time and encountered an error in JSP page translation.
Compilation of JSP page's servlet class : This is the
second step of JSP life cycle when a request is mapped to a JSP page first
time. A JSP page is entered into this phase after its successfully
translation as in first phase. In this phase the translated Servlet class is
compiled. An exception JasperException with the message containing the JSP
page's servlet name and the encountered error line may be generated at this
phase by a server when a JSP page's servlet encountered and error while
compiling. After successfully compilation of the Servlet class it may entered
into the following sub phases :
Container will check whether the instance of the JSP page's servlet is existing. If
not, then the container will control the following operations :
Loading of JSP page's servlet class.
Instantiation of the Servlet class.
Calling of jspInit() method to initialize a servlet instance. This
method can be overridden if there is a requirement to do JSP specific
Container will call the _jspService() method by passing the request and
Destruction of JSP page's servlet : This is the last
phase of JSP life cycle. In this phase the jspDestroy method is invoked by
the container if it doesn't required to keep the JSP page's servlet. This
method can be overridden if there is requirement to perform JSP specific
JSP life cycle methods are as follows :
Following image is demonstrating the life cycle of JSP.