TrainBeaser for iPhone
The moose likes Tomcat and the fly likes Custom error messages per application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Reply Bookmark "Custom error messages per application" Watch "Custom error messages per application" New topic
Author

Custom error messages per application

Tyler Wassell
Greenhorn

Joined: Dec 09, 2011
Posts: 8
Hello, I am trying to configure my tomcat web.xml file with custom 500 error messages. I know that the simple xml syntax to do this is something like this:
<error-page>
<error-code>500</error-code>
<location>/preview_disabled.html</location>
</error-page>

But, I have many applications that use this web.xml config and I want to only use this error page for a couple of the applications. Can anyone tell me if there is syntax to specify this error page for 500 errors thrown by specific applications? For examaple, my class is TRCPS.PREVIEWINQ. I tried the following but it applied the rule to all applications in the context:
<error-page>
<servlet-name>PREVIEWINQ</servlet-name>
<servlet-class>MRCPS.PREVIEWINQ</servlet-class>
<error-code>500</error-code>
<location>/preview_disabled.html</location>
</error-page>

Thanks!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 12513

Welcome to the JavaRanch, Tyler!

If you want to supply a custom error page on a per-application basis, you do that in the application's web.xml file, a/k/a the Server-Independent Deployment Descriptor. Most of us, never, in fact, modify the Tomcat global web.xml at all. In fact, I'd worked with Tomcat for close to 10 years before I even realized it was there, since it's Tomcat-specific and not part of the J2EE spec.


One of the most odious afflictions that Business has inflicted on the modern English language is "pro-active". Most of the time it's simply redundantly used in place of the simple old word "active". And a good deal of the rest of the time it means "You're not overworked enough yet, so go out and find more!"
Tyler Wassell
Greenhorn

Joined: Dec 09, 2011
Posts: 8
Thank you for taking the time to respond.

I am actually not using the global Tomcat web.xml file, I should have been more clear. I have defined a servlet context outside of Tomcat and that context has its own web.xml file in c:\installpath\context\WEB-INF. I guess what I am saying is that I have to deploy many servlets in this context and they all use this web.xml. However, I do not want every one of these servlets to use the same custom error config that I described above. I just want specific servlets to use the errorconfig in this web.xml file. Each individual servlet program does not have its own web.xml file....
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 12513

Ah.

The location of the webapp doesn't matter. Tomcat will treat any deployed webapp the same way as any other deployed webapp. However, the directives in web.xml apply to the webapp as a single application (which technically, it is).

So to provide alternative error pages, you'll have to do some work.

1. You can have the exception be caught by a JSP, instead of an HTML page and put logic in the JSP to redirect to the desired error page.

2. You can have the servlets trap the error and redirect to the error page directly. Which I recommend, because it offers the ability to better report and clean up on failues. Doubly so if the majority of errors go to standard pages, and you only want selected servlets to use per-servlet pages. Variation: subclass the HttpServlet base class, add the desired error handling, then use that class as the base class for your servlets.

3. You can code a ServletFilter and have it trap and redirect responses going out with a "500" Response Code.
Tyler Wassell
Greenhorn

Joined: Dec 09, 2011
Posts: 8
Tim, Thanks for the recommendations. I went the JSP route and all is working well.
 
 
subject: Custom error messages per application
 
Threads others viewed
Custom Error Page for Error Code 500 in Tomcat
Custom error pages
JSP and custom Exception class
Custom Error Pages (404 & 500) not invoked
Redirect internal server error to custom JSP
IntelliJ Java IDE