Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

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 11.

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:

If you would like the Sun Java System Application Server to keep the generated servlets for a Web module in deploytool, perform these steps:

  1. Select the WAR.
  2. Select the General tab.
  3. Click the Sun-specific Settings button.
  4. Select the Servlet/JSP Settings option from the View combo box.
  5. Click the Add button in the JSP Configuration frame.
  6. Select keepgenerated from the Name column.
  7. Select true from the Value column.
  8. Click Close.

In the Application Server, the source for the servlet created from a JSP page named pageName is in this file:

<J2EE_HOME>/domains/domain1/generated/
  jsp/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

<J2EE_HOME>/domains/domain1/generated/
  jsp/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.

After the page has been translated and compiled, the JSP page's servlet (for the most part) 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
    1. Loads the JSP page's servlet class
    2. Instantiates an instance of the servlet class
    3. 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.

Execution

You can control various JSP page execution parameters by using page directives. The directives that pertain to buffering output and handling errors are discussed here. Other directives are covered in the context of specific page-authoring tasks throughout the chapter.

Buffering

When a JSP page is executed, output written to the response object is automatically buffered. You can set the size of the buffer using the following page directive:

<%@ page buffer="none|xxxkb" %> 

A larger buffer allows more content to be written before anything is actually sent back to the client, thus providing the JSP page with more time to set appropriate status codes and headers or to forward to another Web resource. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

Handling Errors

Any number of exceptions can arise when a JSP page is executed. To specify that the Web container should forward control to an error page if an exception occurs, include the following page directive at the beginning of your JSP page:

<%@ page errorPage="file_name" %> 

The Duke's Bookstore application page prelude.jsp contains the directive

<%@ page errorPage="errorpage.jsp"%> 

The following page directive at the beginning of errorpage.jsp indicates that it is serving as an error page

<%@ page isErrorPage="true" %> 

This directive makes an object of type javax.servlet.jsp.ErrorData available to the error page so that you can retrieve, interpret, and possibly display information about the cause of the exception in the error page. You access the error data object in an expression language (see Expression Language) expression via the page context. Thus, ${pageContext.errorData.statusCode} is used to retrieve the status code, and ${pageContext.errorData.throwable} retrieves the exception. If the exception is generated during the evaluation of an EL expression, you can retrieve the root cause of the exception using this expression:

${pageContext.errorData.throwable.rootCause} 

For example, the error page for Duke's Bookstore is as follows:

<%@ page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
  prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" 
  prefix="fmt" %>
<html>
<head>
<title><fmt:message key="ServerError"/></title>
</head>
<body bgcolor="white">
<h3>
<fmt:message key="ServerError"/>
</h3>
<p>
${pageContext.errorData.throwable}
<c:choose>
  <c:when test="${!empty
    pageContext.errorData.throwable.cause}">
    : ${pageContext.errorData.throwable.cause}
  </c:when>
  <c:when test="${!empty
    pageContext.errorData.throwable.rootCause}">
    : ${pageContext.errorData.throwable.rootCause}
  </c:when>
</c:choose>
</body>
</html> 

Note: You can also define error pages for the WAR that contains a JSP page. If error pages are defined for both the WAR and a JSP page, the JSP page's error page takes precedence.


Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.