File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Custom error page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Custom error page" Watch "Custom error page" New topic
Author

Custom error page

Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Anyway to override Tomcat default error page? I am not talking about the error-page directive, but if a critical error occurs (out of memory, context not found, etc..) I would like to customize the page it shows.


http://www.goodercode.com
SCJP 1.4
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Best what you can do is to create an error-page for the HTTP status 500 or the java.lang.Exception class.
For a java.lang.Error there is not much room to handle it. It's already too late then.
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
Bauke Scholtz wrote:Best what you can do is to create an error-page for the HTTP status 500 or the java.lang.Exception class.
For a java.lang.Error there is not much room to handle it. It's already too late then.


so I guess you are saying there is no way to change the 'oh no everything is broken and I am going to tell the world about it' page?
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
anybody? my problem is when the exception page has an exception. I want zero chance of showing a stacktrace to my visitors.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Make the exception page robust. Do for instance not clutter it with scriptlets.
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
that is fine, I understand the exception page should not throw exceptions or whatever. I just need to be able to guarantee 100% there will never be an Apache/Tomcat error report. I am fronting with Apache, but if I put a 500 ErrorDocument in apache, it will interfere with my existing dynamic 500 pages.

I am currently writing a CustomErrorReportingValve. For those interested, the code for the current exception page can be found in org.apache.catalina.valves.ErrorReportValve report method.
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
OK, here is my solution:

ErrorReportValve class handles the output of error reporting pages. This is automagically instantiated when the server is started. However, we can define an errorReportValveClass on the Host element and use our own class. So I created a class that converts all unhandled errors to 503 (service unavailable) because we are catching these at the web server in case tomcat is down. That way the ErrorDocument directive will handle it.

Maybe not the most ideal way to handle it, but this will completely remove the possibility of end users seeing the tomcat error report page.

Here is my code, it is based off the default ErrorReportValve:



and in server.xml:

Al Rodgers
Greenhorn

Joined: Mar 28, 2012
Posts: 1
A variation on the above approach is to make your custom valve class extend Tomcat's org.apache.catalina.valves.ErrorReportValve and just override the report method. Copy the existing implementation of this method from the Tomcat source then adapt it to write your custom error page HTML as desired (e.g. excluding the container description and exception stack trace for security-sensitive environments).

Finally, configure this valve on the host as described previously, e.g.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom error page
 
Similar Threads
September 2003 Newsletter Article Discussion - The Coffee House
Url to app server
errorPage doesn't got to new page
Error Page problem?
Session timeout Problem in Richfaces Model Panel?