How do I use Tomcat's error-page and Apache's ErrorDocument directives together?
Created May 7, 2012
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:
<web-app>
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]
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
...
<web-app>