How do I use Tomcat's error-page and Apache's ErrorDocument directives together?

Alessandro A. Garbagnati

I suggest you to have both Apache and Tomcat know how to hande "their own" error documents. The reason for this is because you could have a situation where an error is generated by Tomcat and cases when a same error is generated by Apache.

A simple example is with the Page Not Found (404) error:

In a normal system you have Tomcat handling only servlets (usually /servlets/*) and jsp pages (*.jsp). Then when Apache receives a request that matches one of those directive, it will pass it to Tomcat. If a servlet or a jsp file don't exists, then the 404 error is generated by Tomcat.

At the same time, if the request contains page that is not a jsp or a servlet (maybe a typo: "index.jps"), then the 404 error is generated by Apache.

What I normally do is to have a jsp.page to handle the 404 (and others) error. Then tell Tomcat how to handle it through the web application descriptor (web.xml) adding these lines:


and then, add the ErrorDocument directive to Apache (inside the configuration file, usually httpd.conf) that points to this page:

ErrorDocument 404 http://www.foo.com/error404.jsp

Note: You need to be sure that the page you specify does not begin with a number (i.e.: 404.jsp). This because, according to Java Syntax, you cannot start a class name with a number.

[See also How can I write an "error page" -- that is, a serv... in the Servlets FAQ -Alex]