The Java EE 5 Tutorial


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.


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:

<%@ 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 EL (see Unified Expression Language) expression by way of the page context. Thus, ${pageContext.errorData.statusCode} retrieves the status code, and ${pageContext.errorData.throwable} retrieves the exception. You can retrieve the cause of the exception using this expression:


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

<%@ page isErrorPage="true" %>
<%@ taglib uri=""
     prefix="c" %>
<%@ taglib uri=""
     prefix="fmt" %>
<title><fmt:message key="ServerError"/></title>
<body bgcolor="white">
<fmt:message key="ServerError"/>
: ${pageContext.errorData.throwable.cause}

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.