views:

1242

answers:

2

I would like to use Tomcat's error-page directive to display various different error pages in response to various types of exceptions. However, I want the error page displayed to have different styling and content depending on the original request URL that resulted in the error.

Specifically, I have an admin part of my web application, and a user part. The error pages should be different, both in terms of styling and menus. The most reliable way to distinguish which part a given page is in is by looking for strings in the URL. I'd like to point the Tomcat error-page to, say, a servlet that would parse the URL and redirect to the appropriately styled error page.

So 1) is it possible to use error-page, or some other mechanism, to redirect errors to a servlet rather than a JSP? (And if not, can I do what I'm imagining within a JSP?)

and 2) once I'm in my servlet, can I determine the request URL that led to the error (request.requestURI doesn't appear to work -- it points to the location of the error JSP itself).

A: 

Tomcat's error page is the last resort for showing application errors(just in case your error page fails actually).Usually you should(I would recommend) to build your own error page where you can display errors in a non-technical view(by default) and technical view(if the user wants) in such a manner that is suitable for your users.

adrian.tarau
Fair point, and in fact we have done that; this application is based on Spring MVC and uses Spring's exception resolving for errors that aren't caught and handled more locally. However, certain errors that happen outside of the application and framework scope still hit Tomcat; the specific type that seems to come up often is a syntax error of some kind within a JSP.
JacobM
True, in this case you should relay on jimr's answer. We are not using JSP so everything wrong happens we can catch it and display it.
adrian.tarau
+2  A: 

You can get the original requestURI from

pageContext.errorData.requestURI

In your error jsp that you register via <error-page> in your web.xml

See http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/ErrorData.html for more information.

As for using a servlet, you can probably use a <jsp:forward> inside the error jsp to forward to a servlet of your choosing.

jimr
Now we're getting somewhere. I'll hold off on marking this as answered until 1) I've tried it, and 2) I give a bit more time for responses, but this is definitely the type of information I was looking for. Thanks.
JacobM
Worked like a charm (I'm doing a c:if checking whether the requestURI contains certain strings, and doing a forward if it does.
JacobM