Oracle JavaServer Pages Developer's Guide and Reference Release 8.1.7 Part Number A83726-01 |
Library | Service | Contents | Index

## JSP Runtime Error Processing

While a JSP page is executing and processing client requests, runtime errors can occur either inside the page or outside the page (such as in a called JavaBean). This section describes the JSP error processing mechanism and provides a simple example.

### Using JSP Error Pages

Any runtime error encountered during execution of a JSP page is handled using the standard Java exception mechanism in one of two ways:

- You can catch and handle exceptions in a Java scriptlet within the JSP page itself, using standard Java exception-handling code.

- Exceptions you do not catch in the JSP page will result in forwarding of the request and uncaught exception to an error page. This is the preferred way to handle JSP errors.

You can specify the URL of an error page by setting the `errorPage` parameter in a `page` directive in the originating JSP page. (For an overview of JSP directives, including the `page` directive, see "Directives". )

In a servlet 2.2 environment, you can also specify a default error page in the `web.xml` deployment descriptor with instructions such as the following:

    <error-page>
       <error-code>404</error-code>
       <location>/error404.html</location>
    </error-page>

(See the Sun Microsystems Java Servlet Specification, Version 2.2 for more information about default error pages.)

An error page must have a `page` directive setting the `isErrorPage` parameter to `true`.

The exception object describing the error is a `java.lang.Exception` instance that is accessible in the error page through the implicit `exception` object.

Only an error page can access the implicit `exception` object. (For information about JSP implicit objects, including the `exception` object, see "Implicit Objects".)

See the next section, "JSP Error Page Example", for an example of error page usage.

Note: There is ambiguity in the JSP 1.1 specification regarding exception types that can be handled through the JSP mechanism. A page implementation class generated by the OracleJSP translator can handle an instance of the `java.lang.Exception` class or a subclass, but cannot handle an instance of the `java.lang.Throwable` class or any subclass other than `Exception`. A `Throwable` instance will be thrown by the OracleJSP container to the servlet container. The ambiguity is expected to be addressed in the JSP 1.2 specification. OracleJSP behavior will be modified appropriately in a future release.

### JSP Error Page Example

The following example, `nullpointer.jsp`, generates an error and uses an error page, `myerror.jsp`, to output contents of the implicit `exception` object.

##### Code for nullpointer.jsp

    <HTML>
    <BODY>
    <%@ page errorPage="myerror.jsp" %>
    Null pointer is generated below:
    <%
       String s=null;
       s.length();
    %>
    </BODY>
    </HTML>

##### Code for myerror.jsp

    <HTML>
    <BODY>
    <%@ page isErrorPage="true" %>
    Here is your error:
    <%= exception %>
    </BODY>
    </HTML>

This example results in the following output:

Note: The line "Null pointer is generated below:" in `nullpointer.jsp` is not output when processing is forwarded to the error page. This shows the difference between JSP "include" and "forward" functionality--with a "forward", the output from the "forward-to" page replaces the output from the "forward-from" page.

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index