Saturday, October 30, 2010

Tomcat Custom 404 Page

This post will cover creating a custom 404 Error page for Tomcat.

By default, if a requested page does not exist, a 404 Error is raised and the user is served Tomcat's default 404 error page as below:

Above, I requested a page called 'david' which does not exist.

To make your 404 page a bit more presentable, provide the user with a link back into your site, as well as hide the Tomcat version in use, you can easily add a custom 404 error page.

Start by creating the page you wish to use. In my case, I am creating a page called 'NotFound.jsp'

Add the 404 page you crated to your site directory.

Now, in your $CATALINA_HOME/conf directory, edit your web.xml file.

Add the following entry just beneath the welcome-file-list entry as shown below:



Now, when someone goes to a page that does not exist, they get the custom 404 page we created.

for example:

SEO Disclaimer: I've called my page NotFound.jsp page and have added a meta-tag:

meta content="noindex" name="robots">

I did this as it (supposedly), the tag will stop search engined from indexing my 404 page.  I'm not an SEO guy so whether this is necessary  (or even a good idea), I don't know.  You could, of course, place your 404 page within a sub directory (e.g. '404') and adjust the location in your web.xml from /404/NotFound.jsp to /NotFound.jsp. You could then add a robots.txt file to prevent crawling of the directory.

Related Posts:
Install Tomcat 6 on CentOS
Tomcat Oracle JDBC Connection
Tomcat Manager Password

No comments: