O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.6. Declaring a Special Exception-Handling JSP for Other JSPs

Problem

You want to declare inside of a JSP that another or external JSP will handle any thrown errors.

Solution

Set the page directive attribute errorPage to the special JSP error page's path in the web application. The JSP error page itself has its page directive attribute isErrorPage set to "true".

Discussion

The JSP specification allows a JSP author to declare at the top of the page that a special error-handling JSP will handle any exceptions thrown by the page that they are authoring. This design allows the encapsulation of error handling inside a specially designed JSP.

If you want to specifically target a JSP error page within JSP code, set the page directive's errorPage attribute to the target error page's location in the web application. Example 9-7 shows a JSP with a page directive declaring errHandler.jsp as its error page.

Warning

This page directive declaration overrides any matching error-page configurations in web.xml. If this JSP throws an java.io.IOException and web.xml has an exception-type attribute for that exception, the web container invokes the error page specified by the page directive instead of any URI specified in the web.xml configuration.

Example 9-7. A JSP that specifies another JSP as its error page

<%@page errorPage="/errHandler.jsp" %>
<html>
<head><title>Exception Thrower</title></head>
<body>
<h2>Throw an IOException </h2>
 <% java.io.File file = new java.io.File("z:" + System.getProperty("file.separator") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required