Wednesday, February 19, 2014

Error redirection pages in Apache Tomcat

Sponsored Links
Errors are part of any application. Broken links and exceptions can be found in any systems. But its bad when some broken links shows 404 page not found  with the default Apache / Tomcat error pages. Revealing information about your webservers. Also its not that great to see a Normal text like Apache error notification when you were browsing a nice website.

So its better to create your own error redirection pages. You can specify link has removed or not found. Also you can provide links to the home page or provide a search box to search things in your website.

In this post we will see how to add custom error redirection pages in Apache tomcat. The version I used is apache-tomcat-6.0.37. On Centos 5.4.

Suppose you have a Apache Tomcat server and more than one web applications inside your webapps directory. Then you will have to add custom pages in each web applications.

For example:
I have a webapps directory and it has three applications in it.

webapps -> portal,sms,crm

So if I set error pages in portal, if the error is happening in the webapp sms, it wont the redirect pages we created for portal. So we need to configure in the three applications separately.

Well. How to do it?

If you want to configure error redirection pages for portal,
Create your pages eg: 404.jsp, 500.jsp etc.
And specify it in your web.xml of the corresponding application.
If it is portal usually it will be portal/WEB-INF/web.xml

You have to set it as follows.
<error-page>
<error-code>400</error-code>
<location>/html/404.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/html/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/html/oops.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/html/oops.html</location>
</error-page>
Be cautious when you give the path. Here the html directory is under portal directory.

But these error pages will show only when the error happens inside the web applications.

To show these for the urls as below,

domain.com/dfgghghg

Add the same error files in ROOT webapp and configure the web.xml under ROOT/WEB-INF

Restart the tomcat and you are done. :) type a broken url and test it.
Sponsored Links

No comments:

Post a Comment

Be nice. That's all.