Before we end the exploration of the combination of JSP and servlets, I’d like to give you one more useful tip, namely how to use a JSP error page that displays a user-friendly error page for all runtime errors, no matter if they originate in a JSP page, a servlet, or a filter.
In Chapter 9, I showed you how to use the
errorPage attribute to specify a JSP page that is
invoked in case an exception is thrown while processing the page. I
also mentioned that an alternative is to declare an error page in the
deployment descriptor (the
file). It’s then used for exceptions thrown by a
servlet, a filter, or a JSP page that doesn’t
declare an error page:
<error-page> <exception-type>java.lang.Throwable</exception-type> <location>/errorpage.jsp</location> </error-page>
To recap, the
contains the fully qualified name of the type of exception you want
to handle with the servlet, JSP page, or static page specified by the
<location> element. The
<location> value must be a context-relative
path (starting with a slash). You can use multiple
<error-page> elements to use different pages
for different exceptions, and the container picks the one with the
<exception-type> element that most closely
matches the type of the exception thrown.
There’s one problem, though: if you use a JSP page to handle the exception, you can’t log or display the exception message, because a JSP page invoked this way doesn’t have access ...