[ Team LiB ] Previous Section Next Section

Recipe 9.6 Declaring a Special Exception-Handling JSP for Other JSPs

Problem

You want to declare inside of a JSP that another or external JSP will handle any thrown errors.

Solution

Set the page directive attribute errorPage to the special JSP error page's path in the web application. The JSP error page itself has its page directive attribute isErrorPage set to "true".

Discussion

The JSP specification allows a JSP author to declare at the top of the page that a special error-handling JSP will handle any exceptions thrown by the page that they are authoring. This design allows the encapsulation of error handling inside a specially designed JSP.

If you want to specifically target a JSP error page within JSP code, set the page directive's errorPage attribute to the target error page's location in the web application. Example 9-7 shows a JSP with a page directive declaring errHandler.jsp as its error page.

This page directive declaration overrides any matching error-page configurations in web.xml. If this JSP throws an java.io.IOException and web.xml has an exception-type attribute for that exception, the web container invokes the error page specified by the page directive instead of any URI specified in the web.xml configuration.


Example 9-7. A JSP that specifies another JSP as its error page
<%@page errorPage="/errHandler.jsp" %>
<html>
<head><title>Exception Thrower</title></head>
<body>
<h2>Throw an IOException </h2>
 <% 
 java.io.File file = new java.io.File("z:" + System.getProperty("file.separator") + 
 "temp");
 file.createNewFile( );  %>
</body>
</html>

The error page has access to an exception implicit object that represents the java.lang.Throwable object associated with the error.

Example 9-8 uses the JSTL and the EL to show information about the exception. See Chapter 23 if you have not yet been introduced to the JSTL or the EL.

Example 9-8. A JSP error page named errHandler.jsp
<%@page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head><title>Sorry about the error</title></head>
<body>
<h2>Sorry, We Erred Handling Your Request</h2>
<strong>Here is information about the error:</strong> <br><br>

The servlet name associated with throwing the exception: 
<%-- JSP 2.0 usage only! 
<c:out value="${pageContext.errorData.servletName}" />  --%>
<br><br>
The type of exception: 
  <c:out value="${pageContext.exception.class.name}" />
<br><br>
The request URI:
<%-- JSP 2.0 usage only! 
<c:out value="${pageContext.errorData.requestURI}" />  --%>
<br><br>
The exception message: 
  <c:out value="${pageContext.exception.message}" />
 </body>
</html>

Figure 9-5 shows the errHandler.jsp page displayed in a browser, after the JSP in Example 9-7 has thrown a java.io.IOException while trying to create a file on a phantom disk. The commented-out sections of Example 9-8 show the use of the javax.servlet.jsp.ErrorData class, which allows you to use the EL to get more information about the error. For example, you can get the request URI (as in /home/errGen.jsp) of the offending JSP with this syntax:

 ${pageContext.errorData.requestURI}

However, this usage fails in a JSP 1.2 container such as Tomcat 4.1.12, because it was introduced in JSP 2.0. This is why there is an empty space in the browser page after "The request URI:."

Figure 9-5. A JSP error page using the page directive attributes errorPage and isErrorPage
figs/jsjc_0905.gif

You can also use this syntax to get access to the java.lang.Throwable object in the error page:

<c:out value="${requestScope[
   \"javax.servlet.jsp.jspException\"]}" />

See Also

Recipe 9.1 on declaring error pages in web.xml; Recipe 9.2 on creating a special exception-handling servlet; Recipe 9.3 on sending an error from a servlet; Recipe 9.4 on sending an error from a JSP; Recipe 9.5 on using JSPs to handle errors; Chapter 23 on using the JSTL; Chapter JSP.1.4 of the JSP 2.0 specification on error handling: http://java.sun.com/products/jsp/.

    [ Team LiB ] Previous Section Next Section