4

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.

3 Answers 3

5

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.

6
  • Eli, thank you. It is still showing the same behavior, even when I put the full path to error.html
    – Mr Aleph
    Mar 31, 2011 at 13:21
  • Ok. I managed to make it work for standard html errors (400, 404, etc). I just needed to place the error.html in the correct location. Now for the servlet being called without arguments: it still offers me to download it. Any ideas?
    – Mr Aleph
    Mar 31, 2011 at 14:02
  • Check your directory settings, it is skipping my mind right now, but there is essentially a directive command which has to be changed in order to "open in browser" not "download" files
    – Eli
    Mar 31, 2011 at 14:05
  • Thanks again but I am working blind right now since I have no idea where to even begin looking
    – Mr Aleph
    Mar 31, 2011 at 14:34
  • I'm trying a few things, but here is a good beginner tutorial on servlets that may possibly help get you headed in the right direction: cs.calstatela.edu/~abbott/Courses/CS_320b/…
    – Eli
    Mar 31, 2011 at 14:47
1

to capture 404 for www.server.com/servlet1/SomRandomStuff move your error.html file to $CATALINA_HOME/webapps/servlet1/error.html. copy the error file to each of your webapp. Even though not the best way to handle this, it worked for me.

0

Your error file should sit in this directory (not root):

$CATALINA_HOME/webapps/servlet1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .