The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

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 and much of the discussion in this chapter refers to functions described in Java Servlet Technology.

When a request is mapped to a JSP page, it is handled by a special servlet that first checks whether the JSP page's servlet is older than the JSP page. If it is, it 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. Template data is transformed into code that will emit the data into the stream that returns data to the client. JSP elements are treated as follows:

For a JSP page named pageName, the source for a JSP page's servlet is kept in the file:

J2EE_HOME/repository/host/web/host_port%2Fcontext root/_0002fpageName_0002ejsppageName_jsp_v.java 

Each time you change a JSP page, the web container generates a new Java source file and increments the version number v.

For example, the source for the index page (named index.jsp) for the date localization example discussed at the beginning the chapter would be named:

J2EE_HOME/repository/host/web/host_8000%2Fdate/_0002findex_0002ejspindex_jsp_v.java 

Both the translation and compilation phases can yield errors that are only observed when the page is requested for the first time. If an error occurs while the page is being translated (for example, if the translator encounters a malformed JSP element), the server will return a ParseException and the servlet class source file will be empty or incomplete. The last incomplete line will give a pointer to the incorrect JSP element.

If an error occurs while the JSP page is being compiled (for example, due to a syntax error in a scriptlet), the server will return a JasperException and a message that includes the name of the JSP page's servlet and the line where the error occurred.

Once 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. Invokes the _jspService method, passing a request and response object.

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 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 adjust the size of the buffer with 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 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 initdestroy.jsp contains the directive

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

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

<%@ page isErrorPage="true|false" %> 

This directive makes the exception object (of type javax.servlet.jsp.JspException) 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.


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.

Home
TOC
PREV TOC NEXT Search
Feedback