Sunday, August 1, 2010

Runtime exception in JSP

How does JSP handle run-time exceptions?

Runtime exceptions are easy to handle in a JSP application, because they are stored one at a time in the implicit object named exception. You can use the exception object in a special type of JSP page called an error page, where you display the exception's name and class, its stack trace, and an informative message for your user.

We can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example:

<\%@ page errorPage="error.jsp" %>

redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing.

Within error.jsp, if you indicate that it is an error-processing page, via the directive:

<\%@ page isErrorPage="true" %>

the Throwable object describing the exception may be accessed within the error page via the exception implicit object.

Note: You must always use a relative URL as the value for the errorPage attribute.

No comments:

Post a Comment