TomcatErrorPage

HomePage | RecentChanges | Preferences

Setting up Custom Error Pages in Tomcat

Despite having this in web.xml:

   <error-page>
      <exception-type>javax.servlet.ServletException</exception-type>
      <location>/servletException.jsp</location>
   </error-page>

I was still getting this:

HTTP Status 500 - 
type Exception report
message 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception 
javax.servlet.ServletException: blah, blah...
...
root cause org.example.DAOException: blah, blah...

The error is coming from code that looks like this:

  try { 
    ...
  } catch ( Exception ex ) {
    throw new DAOException( ex );
  }

It turns out that you need to configure an error page for the _inner_ exception! In this case:

   <error-page>
      <exception-type>org.example.DAOException</exception-type>
      <location>/servletException.jsp</location>
   </error-page>

You can have these JSP's stored under WEB-INF, such as

   <error-page>
      <exception-type>org.example.DAOException</exception-type>
      <location>/WEB-INF/jsp/error/servletException.jsp</location>
   </error-page>

You can NOT use a Tiles definition for the <location> because things in web.xml don't know about Struts/Tiles?. See: StrutsException


With nothing in struts-config.xml and this in web.xml

   <error-page>
      <exception-type>Exception</exception-type>
      <location>/WEB-INF/jsp/error/exception.jsp</location>
   </error-page>

or

   <error-page>
      <exception-type>javax.servlet.ServletException?</exception-type>
      <location>/WEB-INF/jsp/error/exception.jsp</location>
   </error-page>

This happens:

HTTP Status 500 - 
type Exception report
message 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception 
javax.servlet.ServletException: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
...
root cause 
com.example.DAOException: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
...
Caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.

Asked on tomcat-user 02/25/2005 to see why I can't trap a ServletException?.

Answer: The Servlet spec says that if it's a ServletException?, the container will extract the wrapped Exception. So that's why I can't catch the ServletException?.

Fix: Use <error-code>500</error-code> to trap "everything else"


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited February 25, 2005 3:03 pm by WendySmoak (diff)
Search: