Custom Error Page in Tomcat

Posted: Friday, September 19, 2014 by జగదేశ్ in Labels: ,
0

Recently we were faced with a issue where we need to configure a Custom error page for tomcat. When there is a issue with any of the application running in tomcat or tomcat going down, we usually used to show error pages from  the front end web server.

But we want to give the application teams more information on the error and also information like the tomcat name where the application was running and also more information on the error.

While trying to configure a Generic 404 Page for all application running inside a Tomcat Container , Here are my findings

1. Custom Error Pages can be configured from the Server side but it will not work for all applications running. We can write our own Error page and ask the application teams to include that error page in their war files ( also enable in the web.xml )

2. Another Option is to develop a sample web application which holds the Custom Error pages and display then whenever we see Exceptions but this requires to push application to all existing containers

3. The Third Option since we use the web server proxies traffic back to Tomcat.  we can use the ProxyErrorOverride directive in Apache HTTP server and with this setting the web server intercepts any traffic coming back from Tomcat that has an error status code (codes 400-599) and replaces it with custom error pages you define on the web server layer.

4. The other options is to customize the Error Reporting. We do have something called as Valve available in Tomcat Container. The Valve Element represents a Component that will be inserted into the request processing for a associated Cataline container like Engine , Host and Context. We are provided with a Valve called ErrorReportValve Which helps in generating Custom Error Messages. I have  Written a Sample code by extending the ErrorReportValve.

Here is the Code

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ErrorReportValve;

public class ErrorPage extends ErrorReportValve{

    @Override
    protected void report(Request request, Response response, Throwable t) {
        try {
                  
                   int statusCode = response.getStatus();
                   PrintWriter out=response.getWriter();
                   out.write("<html><head><title>Error Page : 404</title><body>");
                   out.write("Date : "+new java.util.Date());
                   out.write("Instance Name : "+System.getProperties().getProperty("jbs.name","unknown"));
                   out.write("Description : Requested Resource is Not Available");
                   out.write("</body></html>");
                  
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Make a Jar of this Code , copy to the Server/Lib. Now add the Valve Element to the Host in Tomcat Server.xml file as

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false" errorReportValveClass="com.uprr.custom.error.ErrorPage">
</Host>


Now when ever there is 404 , the Custom error message is Shown as