根据原始请求的URL显示不同的Tomcat错误页面

时间:2022-10-04 19:19:23

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.

我想使用Tomcat的error-page指令来显示各种不同的错误页面以响应各种类型的异常。但是,我希望显示的错误页面具有不同的样式和内容,具体取决于导致错误的原始请求URL。

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.

具体来说,我有我的Web应用程序的管理员部分和用户部分。在样式和菜单方面,错误页面应该不同。区分给定页面所在部分的最可靠方法是在URL中查找字符串。我想将Tomcat错误页面指向一个servlet,该servlet将解析URL并重定向到适当样式的错误页面。

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

那么1)是否可以使用错误页面或其他一些机制将错误重定向到servlet而不是JSP? (如果没有,我可以在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).

2)一旦我进入我的servlet,我能否确定导致错误的请求URL(request.requestURI似乎不起作用 - 它指向错误JSP本身的位置)。

2 个解决方案

#1


You can get the original requestURI from

您可以从中获取原始requestURI

pageContext.errorData.requestURI

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

在您通过web.xml中的 注册的错误jsp中

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

有关更多信息,请参阅http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/ErrorData.html。

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

至于使用servlet,你可以在错误jsp中使用 转发到你选择的servlet。

#2


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.

Tomcat的错误页面是显示应用程序错误的最后手段(以防你的错误页面实际失败)。通常你应该(我建议)构建你自己的错误页面,你可以在非技术视图中显示错误(默认情况下) )和技术视图(如果用户想要)以适合您的用户的方式。

#1


You can get the original requestURI from

您可以从中获取原始requestURI

pageContext.errorData.requestURI

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

在您通过web.xml中的 注册的错误jsp中

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

有关更多信息,请参阅http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/ErrorData.html。

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

至于使用servlet,你可以在错误jsp中使用 转发到你选择的servlet。

#2


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.

Tomcat的错误页面是显示应用程序错误的最后手段(以防你的错误页面实际失败)。通常你应该(我建议)构建你自己的错误页面,你可以在非技术视图中显示错误(默认情况下) )和技术视图(如果用户想要)以适合您的用户的方式。