Enhanced error reporting

A servlet can report errors by performing these actions:

The enhanced servlet error reporting function in WebSphere Application Server - Express provides a 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 WebSphere Application Server - Express, 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, perform these steps:

  1. Create the error JSP file.
  2. Place the file in the Web module document root.
  3. Use the WebSphere Development Studio Client to configure an error path for the Web module. For more information, see the WebSphere Development Studio Client Help.

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>

WebSphere Application Server - Express 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. Use the WebSphere Development Studio for iSeries tools to configure the default error page. For more information, see the WebSphere Development Studio Client Help.