background image

The Life Cycle of a JSP Page

<< Book Catalog | Handling JSP Page Errors >>
<< Book Catalog | Handling JSP Page Errors >>

The Life Cycle of a JSP Page

The Life Cycle of a JSP Page
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. You will
notice that many sections in this chapter refer to classes and methods described in
Chapter 4,
"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.
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:
Directives are used to control how the web container translates and executes the JSP page.
Scripting elements are inserted into the JSP page's servlet class. See
Chapter 9, "Scripting in
JSP Pages"
for details.
Expression language expressions are passed as parameters to calls to the JSP expression
evaluator.
jsp:[set|get]Property
elements are converted into method calls to JavaBeans
components.
jsp:[include|forward]
elements are converted into invocations of the Java Servlet API.
The jsp:plugin element is converted into browser-specific markup for activating an applet.
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:
domain-dir/generated/jsp/j2ee-modules/WAR-NAME/pageName_jsp.java
For example, the source for the index page (named index.jsp) for the date localization
example discussed at the beginning of the chapter would be named:
domain-dir/generated/jsp/j2ee-modules/date/index_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.
The Life Cycle of a JSP Page
The Java EE 5 Tutorial · September 2007
142