[ Team LiB ] |
Recipe 9.6 Declaring a Special Exception-Handling JSP for Other JSPsProblemYou want to declare inside of a JSP that another or external JSP will handle any thrown errors. SolutionSet 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". DiscussionThe 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. 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
See AlsoRecipe 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 ] |