[ Team LiB ] Previous Section Next Section

19.6 Using a Common JSP Error Page

Before we end the exploration of the combination of JSP and servlets, I'd like to give you one more useful tip, namely how to use a JSP error page that displays a user-friendly error page for all runtime errors, no matter if they originate in a JSP page, a servlet, or a filter.

In Chapter 9, I showed you how to use the page directive's errorPage attribute to specify a JSP page that is invoked in case an exception is thrown while processing the page. I also mentioned that an alternative is to declare an error page in the deployment descriptor (the WEB-INF/web.xml file). It's then used for exceptions thrown by a servlet, a filter, or a JSP page that doesn't declare an error page:

<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/errorpage.jsp</location>
</error-page>

To recap, the <exception-type> element contains the fully qualified name of the type of exception you want to handle with the servlet, JSP page, or static page specified by the <location> element. The <location> value must be a context-relative path (starting with a slash). You can use multiple <error-page> elements to use different pages for different exceptions, and the container picks the one with the <exception-type> element that most closely matches the type of the exception thrown.

You can also define a custom handler for response status codes other than 200 (i.e., status codes that signal some kind of problem):

<error-page>
  <error-code>404</error-code>
  <location>/notfound.jsp</location>
</error-page>

If you use a JSP page as the handler, it has access to the all information about the request that failed and the reason (the exception or status code) through the properties of the pageContext.errorData variable, as described in Chapter 9. Prior to JSP 2.0, you had to work around a mismatch between the JSP and servlet specifications in order to access exception information in an error handling JSP page: the name of the request attributes used to pass on this information differed between the specifications. Fortunately, the JSP 2.0 specification is aligned with the servlet specification in this regard, so now you can use a JSP page like the one described in Chapter 9 even as a global error handler, without resorting to any tricks.

For a servlet error handler, the error information is available through the request attributes shown in Table 19-2.

Table 19-2. Error information request attributes

Attribute name

Java type

Description

javax.servlet.error.request_uri
String

The context-relative URI for the erroneous request

javax.servlet.error.servlet_name
String

The name of the servlet handling the erroneous request

javax.servlet.error.status_code
int

The status code for the erroneous request

javax.servlet.error.exception
Throwable

The exception thrown by the erroneous request, if any

You can use the error information to display informative messages to the user, or to log it along with information about request parameters, headers, etc., for analysis of the kind of problems your users experience when using the application.

    [ Team LiB ] Previous Section Next Section