Java Reference
In-Depth Information
You will notice that the _jspService() method looks much like any other generated JSP,
except that it has the following lines:
Throwable exception =
(Throwable)request.getAttribute(“javax.servlet.jsp.jspException”);
These two lines make it possible for the error page to access the implicit exception object. It
does this by getting the exception object from the request, using the
request.getAttribute() method with a key of javax.servlet.jsp.jspException . Now
your JSP can do whatever it wants with the received exception. In the next section, we will
examine how the exception object gets placed into the request.
Using a JSP Error Page
Now that you know how to create a JSP error page, let's put one to use. It takes only one addi-
tional attribute, in your page directive, to make your JSP aware of an error page. You simply
need to add the errorPage attribute and set its value equal to the location of your JSP error
page. The JSP in Listing 18.2 uses the error page you created in the previous section.
18
L ISTING 18.2
testerror.jsp
<%@ page errorPage=”errorpage.jsp” %>
<%
if ( true ) {
// Just throw an exception
throw new Exception(“A JSP Exception”);
}
%>
You will notice in this listing that the first line of code sets the errorPage equal to
errorpage.jsp , which is the name of your error page. The rest of the example is used to throw
an exception that will not be caught. That is all there is to it. Copy both of these JSPs to the
<SERVER_ROOT> /djs/ directory and open the testerror.jsp page in your browser. You will
see a page similar to Figure 18.1.
 
Search WWH ::




Custom Search