Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom error messages per application

 
Tyler Wassell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tyler Wassell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18214
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, Thanks for the recommendations. I went the JSP route and all is working well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic