Tomcat – Custom error pages on Apache Tomcat

custom-errorstomcat

I'm running an Apache Tomcat server with 2 custom servlets. The tree structure looks like:

$CATALINA_HOME/webapps/
         ROOT/
           WEB-INF/
         servlet1/
           META-INF/
           WEB-INF/
         servlet2/
           META-INF/
           WEB-INF/

I am trying to add a custom error html page for each servlet that will be displayed on error 400, 404 and when there is an exception on the servlet (such as the information that was supposed to be sent to the servlet on the http request is not there)

I added to $CATALINA_HOME/webapps/servlet1/WEB-INF/web.xml

 <error-page>
   <error-code>400</error-code>
   <location>/error.html</location>
 </error-page>
 <error-page>
   <error-code>404</error-code>
   <location>/error.html</location>
 </error-page>
 <error-page>
   <exception-type>java.lang.Throwable</exception-type>
   <location>/error.html</location>
 </error-page>

and I created error.html in $CATALINA_HOME/webapps/ROOT/. This doesn't seem to be working. When I type" www.server.com/servlet1/SomRandomStuff to get a 404, a blank page is displayed, not error.html. Something else happens when I call the servlet without passing the correct info to it, the browser will ask me if i want to download a file.
I've tried placing error.html not on ROOT but on the servlet1 directory. Didn't work. I checked that error.html has the correct permissions, it is OK. I am out of ideas and the few tutorials I found online are very vague.

What am I doing wrong? How do I achieve this – display a custom error page per servlet?

Thank you for the help.

Best Answer

You have your page paths starting with slashes:

 <error-page>
   <error-code>400</error-code>
   <location>**/error.html**</location>
 </error-page>
 <error-page>
   <error-code>404</error-code>
   <location>**/error.html**</location>
 </error-page>
 <error-page>
   <exception-type>java.lang.Throwable</exception-type>
   <location>**/error.html**</location>
 </error-page>

In this case it might be looking for them in the root of the drive.

If they are in the same folder as web.xml you can try it this way ./error.html

Other than that, I would recommend trying the full path to the file: /var/www/error/error.html or whatever your situation is.