How can I use the same error page for multiple error codes in Tomcat?

I am trying to send text error messages from the tomcat servlet so that the answers can be presented to the user by the application.

I have the following in my web.xml:

<error-page> <error-code>409</error-code> <location>/string_error.jsp</location> </error-page> 

And string_error.jsp looks like this:

 ${requestScope['javax.servlet.error.message']} 

This gives me a plain-text error message for answer 409. However, I would like to use the same page for any error in the 400/500 range, without manually specifying a new <error-page> block for each of them. I would suggest that <error-code>*</error-code> will do this, but it is not. Does Tomcat create a mechanism for this?

+4
source share
2 answers

If you are using a Servlet 3.0 container such as Tomcat 7.0, you can simply omit the <error-code> (or <exception-type> ) element to make it the default global error page.

 <error-page> <location>/string_error.jsp</location> </error-page> 

Since Servlet 3.0, these elements are optional.

However, if you are not already on Servlet 3.0, you will have to configure it at the container level. For example, Tomcat 6.0 (which is a Servlet 2.5 container), you need to create your own error reporting valve class. You can then specify it as the errorReportValveClass attribute of the <Host> element in the /conf/server.xml file.

For other containers, refer to their documentation.

+5
source

You can add error-page elements to either error-code or (java) exception-type in Tomcat. I do not think that this can be done as general as you suggested.

 <error-page> <error-code>404</error-code> <location>/404error.html</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/someJavaException.html</location> </error-page> 
+1
source

All Articles