Recipe 9.5 Creating an Error-Handling JSP


Problem

You want to use a JSP as your error page for both servlets and JSPs.

Solution

Create a JSP that displays information about the java.lang.Throwable reported by using the specified request attributes, such as javax.servlet.error.exception . Use the error-page attribute in web.xml to map certain exception types to the JSP.

Discussion

A JSP can display error information in the same manner as the servlet used in Recipe 9.2. Example 9-5 can be used as the error page for both JSPs and servlets. This sample JSP uses the JSTL and the EL to display the thrown exception's various characteristics, such as its fully qualified class name .

Example 9-5. Using a JSP as an error page
  <%@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:  <c:out value="${requestScope[\"javax.servlet.error.servlet_name\"]}" />  <br><br> The type of exception:  <c:out value=     "${requestScope[\"javax.servlet.error.exception\"].class.name}" />  <br><br> The request URI:  <c:out value="${requestScope[\"javax.servlet.error.request_uri\"]}" />  <br><br> The exception message:  <c:out value=     "${requestScope[\"javax.servlet.error.exception\"].message}" />  </body> </html> 

The error page grabs the request Uniform Resource Indicator (URI), which is the servlet path beginning with the context path and not including any query string, with this code:

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

This passes the value of a request attribute named javax.servlet.error.request_uri to the c:out JSTL tag, which results in the attribute value displayed in the HTML.Make sure to escape the double quotes inside the EL phrase, as in:

 "${requestScope[\"javax.servlet.error.request_uri\"]" 

The code gets information about the exception from the request attributes that are automatically created by the web container when the servlet or JSP throws an exception . For example, if you want to add the response status code to this information, then this number is available from the request attribute javax.servlet.error.status_code .

See Table 9-1 for the complete list of these attributes.


In addition, the part of Example 9-5 that gets the class name of the exception calls getClass( ).getName( ) on the Throwable object. Figure 9-4 shows the browser display of this error page after a JSP named errGen.jsp generates an error.

Figure 9-4. A JSP error page displays information about a thrown exception
figs/jsjc_0904.gif

The web.xml deployment descriptor uses the following element to specify that the error page of Example 9-5 should handle any java.io.IOExceptions :

 <error-page>     <exception-type>java.io.IOException</exception-type >     <location>/errHandler.jsp</location> </error-page> 

Example 9-6 shows the JSP that throws the exception.

Example 9-6. A JSP that throws a java.io.IOException
 <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> 

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.6 on declaring in a JSP that another JSP will handle its exceptions; Chapter 23 on using the JSTL; the JSP 2.0 specification and its Chapter JSP.1.4 on error handling: http://java.sun.com/products/jsp/.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net