When someone hits a page which doesn’t exist on the server, a HTTP 404 error thrown by web server and default 404 error page is shown. With Apache Tomcat, this error page looks like:

Tomcat 404 page

As you can notice that there is no link to go back to home page or any useful message on what went wrong on this page. The default 404 error page in Tomcat is not user friendly. Also, the version of tomcat is displayed which could misused by anyone. In order to make it easy for users to use the web application, it is very common practice to roll out a web application specific 404 error page. In fact the deployment descriptor of web application (web.xml) has tags to specify the location of 404 error page.

Setting application specific custom 404 error page in Tomcat

  • Open web.xml file present in WEB-INF folder of web application.
  • Add the following code at the end of web.xml file:
    <error-page>
       <error-code>404</error-code>
       <location>/error/404.html</location>
    </error-page>
    
  • Restart the server.

Setting generic specific custom 404 error page in Tomcat

You may also want to set a generic custom 404 page which is shown for all web application deployed on the server. However if a web applications has specified its own 404 error page in WEB-INF/web.xml file then that will take precedence.

  • Open web.xml file present in conf folder of apache tomcat.
  • Add the following code at the end of web.xml file (after welcome file list code)
    <error-page>
       <error-code>404</error-code>
       <location>/error/404.html</location>
    </error-page>
    
  • Restart the server.

Sample 404 error messages

Here are a few suggestions on what kind of message can be shown to user on a custom 404 error page:

1) You have reached a page that does not exist or has been moved.
Please check the link used or click on Return to Home below.
Java Experience

2) The requested page was not found on this web application.

Here is how Google shows 404 error pages:

Google 404 page

Show custom error pages in Tomcat admin Tomcat
When someone hits a page which doesn't exist on the server, a HTTP 404 error thrown by web server and default 404 error page is shown. With Apache Tomcat, this error page looks like: As you can notice that there is no link to go back to home page or...
When someone hits a page which doesn't exist on the server, a HTTP 404 error thrown by web server and default 404 error page is shown. With <a href="http://www.javaexperience.com/tomcat-server-interview-questions-and-answers/" title="Tomcat Interview Questions and Answers">Apache Tomcat</a>, this error page looks like: <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/tomcat404.png" alt="Tomcat 404 page"/> As you can notice that there is no link to go back to home page or any useful message on what went wrong on this page. The default 404 error page in Tomcat is not user friendly. Also, the <a href="http://www.javaexperience.com/tomcat-how-to-get-installed-version/">version of tomcat</a> is displayed which could misused by anyone. In order to make it easy for users to use the web application, it is very common practice to roll out a web application specific 404 error page. In fact the deployment descriptor of web application (web.xml) has tags to specify the location of 404 error page. <h2>Setting application specific custom 404 error page in Tomcat</h2> <ul> <li>Open web.xml file present in WEB-INF folder of web application.</li> <li>Add the following code at the end of web.xml file: 1</li> <li>Restart the server.</li></ul> <h2>Setting generic specific custom 404 error page in Tomcat</h2> You may also want to set a generic custom 404 page which is shown for all web application deployed on the server. However if a web applications has specified its own 404 error page in WEB-INF/web.xml file then that will take precedence. <ul> <li>Open web.xml file present in conf folder of apache tomcat.</li> <li>Add the following code at the end of web.xml file (after welcome file list code) 1</li> <li>Restart the server.</li></ul> <h2>Sample 404 error messages</h2> Here are a few suggestions on what kind of message can be shown to user on a custom 404 error page: 1) You have reached a page that does not exist or has been moved. Please check the link used or click on Return to Home below. <a href="http://www.javaexperience.com">Java Experience</a> 2) The requested page was not found on this web application. Here is how Google shows 404 error pages: <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/google404.png" alt="Google 404 page"/>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.