Jay Burgess wrote:
> (I tried this question on the Tomcat list, and got no response.  Maybe it's
> more of a JSP question? Thanks in advance.)

Yes, this is related to the servlet and JSP specs, not a specific
implementation like Tomcat, see below.

>> Can someone explain the relationship between the <error-page> element in
>> my webapp's WEB.XML versus the "errorPage" attribute of the page
>> directive
>> within a JSP?  I'm trying to create a single error page that handles any
>> exceptions generated by the JSPs within my webapp, without having to name
>> the error page in every JSP.
>>
>> To start with, I added the following to my web app's WEB.XML to
>> indicate a
>> "catch all" error page for the app:
>>
>>     <error-page>
>>         <exception-type>java.lang.Exception</exception-type>
>>         <location>/error.jsp</location>
>>     </error-page>
>>
>> I also marked ERROR.JSP as an error page by including the following at
>> the
>> top (note the isErrorPage attribute):
>>
>>     <%@ page contentType="text/html; charset=ISO-8859-1" buffer="64kb"
>> isErrorPage="true" %>
>>
>> Unfortunately, when trying to access the "exception" object in ERROR.JSP,
>> I get a NullPointerException, as though it doesn't exist.
>>
>> Since I thought this should work, I must be missing something?  (I'm
>> using
>> Tomcat 4.0.4, by the way.)

The "errorPage" attribute is defined by the JSP spec, of course, and
the <error-page> element in the web.xml file is defined by the servlet
spec. Both mechanism let you trap and process an exception, but the
the problem is that the servlet spec and the JSP spec use different
names for the request attribute used to pass on the exception to
the error page.

When you use "errorPage", the JSP spec says that the Throwable that
represents the exception must be sent as a request parameter named
"servlet.jsp.jspException". The implicit "exception" variable in the
error page is assigned the value of this request attribute.
The servletc spec says that a request attribute named "javax.servlet.
error.exception" must be used when an exception caught by <error-page>
is forwarded to an error page. Since this does not match the JSP spec
name, it's *not* assigned to the "exception" variable.

I describe a generic solution to this in detail in the upcoming 2nd
edition of my JSP book, but briefly, all you need to do is get hold
of the Throwable from the request attribute used by the servlet spec
instead. With scriptlets, it could look like this:

<%
   if (exception == null) {
     exception = request.getAttribute("javax.servlet.error.exception");
   }
%>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to