Handling exceptions

Exceptions are errors detected within your servlet. Exceptions can occur at run-time, such as when a servlet processes form data, or at compile-time, such as when you pre-compile JSPs. You should catch compile-time exceptions before putting your web application into production. This section describes how to handle run-time exceptions in servlets and JSPs.

In a Java servlet, an exception is represented by an instance of the class javax.servlet.ServletException.

You can define how a web application handles errors using the error-page element in the WEB-INF/web.xml file. You can also define error handling for all web applications on the JRun server by adding error-page elements to the SERVER-INF/default-web.xml file.

The error-page element defines exceptions by exception type or by error code, as the following sections describe. The order of these elements in the web.xml file determines the error handler. JRun redirects the error processing to the location specified by the first error-page element that matches the error-code or exception-type.

Handling Java exceptions

The exception-type subelement of error-page in the web.xml file defines how JRun handles Java exceptions thrown during the processing of a servlet.

You define a fully qualified Java error for the exception-type and then map the error to a destination in the location element. The following example maps FileNotFound exceptions to the 404.jsp page:

<error-page>
  <exception-type>java.io.FileNotFoundException</exception-type>
  <location>/error-pages/404.jsp</location>
</error-page>

The HttpServletRequest and HttpServletResponse objects provide access to error information so you can generate meaningful debugging information or targeted exception handlers. For more information, see "Accessing error attributes".

Handling HTTP error codes

The error-code subelement of error-page in the web.xml file defines how JRun handles HTTP error codes generated during the processing of a servlet.

You define an HTTP status code for the error-code element and then map the code to a destination in the location element. The following example maps the HTTP 500 (Internal Server Error) status code to the servererror.jsp page:

<error-page>
  <error-code>500</error-code>
  <location>/error-pages/servererror.jsp</location>
</error-page>

The following table lists common error-related HTTP status codes:
HTTP error code
Description
400
Bad Request
401
Unauthorized
403
Forbidden
404
Not Found
408
Request Time-out
500
Internal Server Error

For a complete list of error codes, see the HTTP 1.1 RFC at http://rfc.net/rfc2616.html.

The HttpServletRequest and HttpServletResponse objects provide access to error information so that you can generate meaningful debugging information or targeted exception handlers. For more information, see "Accessing error attributes".

Accessing error attributes

The HttpServletRequest and HttpServletResponse objects provide access to error information so that you can generate meaningful debugging information or targeted exception handlers.

JRun sets several attributes on the request object when an error is thrown. The following table describes these attributes:
Attribute
Description
javax.servlet.error.status_code
Defines the HTTP error code, if applicable, as an int object. If the servlet throws an exception not related to HTTP, the status code is usually set to 500 (Internal Server Error).
javax.servlet.error.message
Returns the exception or error message.
javax.servlet.error.exception_type
Defines the type of exception.
javax.servlet.error.exception
Defines the actual exception thrown. You can use the printStackTrace method to view the stack trace of the exception.
javax.servlet.error.request_uri
Defines the request URI prior to the exception being thrown.

The following code example gets the error attributes from the request object and displays them in the browser, including the stack trace:

...
Object status_code = req.getAttribute("javax.servlet.error.status_code");
Object message = req.getAttribute("javax.servlet.error.message");
Object error_type = req.getAttribute("javax.servlet.error.exception_type");
Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
Object request_uri = req.getAttribute("javax.servlet.error.request_uri");
out.println("<HTML><BODY>");
out.println("<B>Status code:</B> " + status_code.toString());
out.println("<BR><B>Message</B>: " + message.toString());
out.println("<BR><B>Error type</B>: " + error_type.toString());
out.println("<BR><B>Request URI</B>: " + request_uri.toString());
out.println("<HR><PRE>");
if (throwable != null) {
  throwable.printStackTrace(out);
}
...

In the web.xml file, define the location of the error handler and the type of error it handles, as the following lines show:

<error-page>
  <exception-type>javax.servlet.ServletException</exception-type>
  <location>/servlet/ErrorHandler</location>
</error-page>

To generate an error that tests this example, create a JSP that includes an expression that divides a number by zero, as the following line shows:

<%= 42/0 %>

To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.