background image

Handling JSP Page Errors

<< The Life Cycle of a JSP Page | Creating Static Content >>
<< The Life Cycle of a JSP Page | Creating Static Content >>

Handling JSP Page Errors

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" on page 102
:
1. If an instance of the JSP page's servlet does not exist, the container:
a. Loads the JSP page's servlet class
b. Instantiates an instance of the servlet class
c. 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 JSP Page 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
tut-install/javaeetutorial5/examples/web/bookstore2/web/template/preludeErrorPage.jspf
contains the directive:
<%@ page errorPage=
"errorpage.jsp"%>
The following page directive at the beginning of
tut-install/javaeetutorial5/examples/web/bookstore2/web/error/errorpage.jsp
indicates that it is serving as an error page:
The Life Cycle of a JSP Page
Chapter 5 · JavaServer Pages Technology
143