• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom error page

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the exception page robust. Do for instance not clutter it with scriptlets.
 
Kerry Wilson
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic