[ Team LiB ] Previous Section Next Section

Recipe 9.2 Creating an Exception-Handling Servlet

Problem

You want to create a servlet that generates an error page.

Solution

Create a servlet that displays some information about the error, then map exception types and/or error codes to the servlet in the deployment descriptor.

Discussion

An error-handling servlet has access to several request attributes that it can use to describe the error. The error page also has access to the request and response objects associated with the page that generated the error. For example, the java.lang.Throwable object associated with any exceptions can be accessed with the following code:

Throwable throwable = (Throwable)
   request.getAttribute("javax.servlet.error.exception");

You can access the server response code with this code:

String status_code = ((Integer)
   request.getAttribute("javax.servlet.error.status_code")).toString( );

Table 9-1 shows the request attributes that an error-handling servlet has access to.

Table 9-1. Request attributes available to servlet error pages

Request attribute

Java type

javax.servlet.error.status_code
java.lang.Integer
javax.servlet.error.exception_type
java.lang.Class
javax.servlet.error.message
java.lang.String
javax.servlet.error.exception
java.lang.Throwable
javax.servlet.error.request_uri
java.lang.String
javax.servlet.error.servlet_name
java.lang.String

Example 9-2 shows the ErrorGen servlet. The web container invokes this servlet when another servlet or JSP throws an unhandled Throwable, according to the configuration in Example 9-1.

Example 9-2. An error-handling servlet
package com.jspservletcookbook;

import javax.servlet.*;
import javax.servlet.http.*;

public class ErrorGen extends HttpServlet {
  
 public void doPost(HttpServletRequest request, 
   HttpServletResponse response)
    throws ServletException, java.io.IOException {
 
    //check the servlet exception       
    Throwable throwable = (Throwable)
      request.getAttribute("javax.servlet.error.exception");

    String servletName = (String)
      request.getAttribute("javax.servlet.error.servlet_name");
    if (servletName == null)
       servletName = "Unknown";
   
    String requestUri = (String)
      request.getAttribute("javax.servlet.error.request_uri");
    if (requestUri == null)
      requestUri = "Unknown";

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter( );
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Error page</title>");  
    out.println("</head>");
    out.println("<body>");

    if (throwable == null){
      out.println("<h2>The error information is not available</h2>");
      out.println("Please return to the <a href=\"" + 
        response.encodeURL("http://localhost:8080/home") + 
          "\">home page</a>.");
   } else{
          out.println("<h2>Here is the error information</h2>");
  
  out.println(
      "The servlet name associated with throwing the exception: "+
        servletName + "<br><br>");

  out.println("The type of exception: " + 
       throwable.getClass( ).getName( ) + "<br><br>");

  out.println("The request URI: " + requestUri + "<br><br>");
  out.println("The exception message: " + throwable.getMessage( ));
   }
  out.println("</body>");
  out.println("</html>");
  
  } 
  
  public void doGet(HttpServletRequest request, 
    HttpServletResponse response)
     throws ServletException, java.io.IOException {

    doPost(request,response);
  }
}

The servlet gets a reference to the thrown exception, then displays information such as the exception's class name and the exception message. The request URI represents a partial path (such as /home/errGen.jsp) to the component that threw the exception, which can be very helpful for debugging and information purposes. Figure 9-1 shows what the browser displays when a servlet throws an exception using Tomcat's web container.

Figure 9-1. Error page HTML displayed by an error-handling servlet
figs/jsjc_0901.gif

Figure 9-2 shows the error page displayed by our example servlet when a JSP in the same web application throws a java.lang.ArithmeticException.

Figure 9-2. The error page displayed by Example 9-1 when a JSP throws an exception
figs/jsjc_0902.gif

See Also

Recipe 9.1 on declaring exception handlers in the deployment descriptor; 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; Recipe 9.6 on declaring in a JSP that another JSP will handle its exceptions; Chapter 1 on the deployment descriptor; the Java servlet specification, which covers error handling in Chapter SRV.9.9: http://java.sun.com/products/servlet/index.html.

    [ Team LiB ] Previous Section Next Section