Enhanced error reporting

A servlet can report errors by performing these actions:

The enhanced servlet error reporting function in WAS provides an different way to implement error reporting. The error page (a JSP file or servlet) is configured for the application and used by all of the servlets in that application. The new mechanism handles caught and uncaught errors.

To return the error JSP file to the client, the Web container performs the following functions:

For WAS, the HttpServletResponse.sendError() method has been overridden to provide the following function:

  public void sendError(int statusCode, String message) {
    ServletException e = new ServletErrorReport(statusCode, message);
    request.setAttribute(ServletErrorReport.ATTRIBUTE_NAME, e);
    servletContext.getRequestDispatcher(getErrorPath()).forward(request, response);
}

To enable this function for your Web module, follow these steps:

  1. Create the error JSP file.
  2. Place the file in the Web module document root.
  3. Use the administrative console to configure an error path for the Web module.

To create an error JSP file, you need to know the public methods of the ServletErrorReport class (the error bean), which are:

  public class ServletErrorReport extends ServletException {

    // Get the stack trace of the error as a string
    public String getStackTrace()

    // Get the message associated with the error.
    // The same message is sent to the sendError() method.
    public String getMessage()

    // Get the error code associated with the error.
    // The same error code is sent to the sendError() method.
    // This will also be the same as the status code of the response.
    public int getErrorCode()

    // Get the name of the servlet that reported the error
    public String getTargetServletName()
}

The following is an example of an error JSP file:

  <BEAN name="ErrorReport" type="com.ibm.websphere.servlet.error.ServletErrorReport"
    scope="request"></BEAN>
  <html>
  <head><title>ERROR: <%= ErrorReport.getErrorCode() %></title></head>
  <body>
  <H1>An error has occurred while processing the servlet named: 
  <%= ErrorReport.getTargetServletName() %></H1>

  <B>Message: </B><%= ErrorReport.getMessage() %><BR>
  <B>StackTrace: </B><%= ErrorReport.getStackTrace() %><BR>
  </body>
  </html>

WAS provides an optional internal servlet, com.ibm.ws.webcontainer.servlet.DefaultErrorReporter, that makes it easier to use the enhanced error reporting capability. You simply add the DefaultErrorReporter servlet and the error JSP (which you develop) to your Web module.

Perform these steps to configure the default error page:

  1. Start the Application Assembly Tool.
  2. Select the WAR file to modify.
  3. Select the Web module.
  4. Select the tab IBM Extensions
  5. Enter the name of the error page in the Default error page field or click Browse to select the error page file.
  6. Click Apply.
  7. Click File --> Save to save the changes.

Perform these steps to configure a specific error page:

  1. Start the Application Assembly Tool.
  2. Select the WAR file to modify.
  3. Select the Web module.
  4. Select Error Pages.
  5. Right-click and select New.
  6. Enter the name of the error page in the Default error page field or click browse to select the error page file.
  7. Select the error the page handles:

    • If the error page should display a particular response code (for example, 404), select Error code and enter the error code in the Error code text field.
    • If the error page should display for a Java exception (for example javax.servlet.UnavailableException), select Exception and enter the exception type in the Exception type name field.

  8. Click OK.
  9. Click File --> Save to save the changes.