Tomcat server, change default HTTP 404?

aDoN picture aDoN · Jan 9, 2015 · Viewed 32.5k times · Source

I am using tomcat and I would like, whenever I go to a direction that is not handled by a servlet, do other stuff insead of showing the default error:

type Status report

message /test

description The requested resource is not available.

Where can I handle this¿?

Thank you in advance

Answer

Stefan picture Stefan · Jan 9, 2015

Define an error page in web.xml:

<error-page>
 <error-code>404</error-code>
 <location>/path/to/your/page.html</location>
</error-page>

Update

  1. No does not need anyting spec. Can be anything, html, jsp, jsf ...
  2. No
  3. Can be placed anywhere, often at the bottom
  4. Between brackets

You can define error pages by their http status (404, 500,...) or by defining the fully qualified exception name (java.lang.Exception, java.io.FileNotFoundException ...). If you are using Servlet 3.x you can even omit the error-code/error-classname part to define a default error page.