Jacob Mattison
Jacob Mattison

Reputation: 51072

Display different Tomcat error pages depending on URL of original request

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).

Upvotes: 2

Views: 3767

Answers (2)

jimr
jimr

Reputation: 11230

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.

Upvotes: 4

adrian.tarau
adrian.tarau

Reputation: 3154

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.

Upvotes: 0

Related Questions