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

发布于 2024-07-25 13:47:48 字数 418 浏览 7 评论 0原文

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

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

那么 1) 是否可以使用错误页面或其他机制将错误重定向到 servlet 而不是 JSP? (如果没有,我可以在 JSP 中执行我想象的操作吗?)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-08-01 13:47:48

获取原始 requestURI,

pageContext.errorData.requestURI

您可以从您通过 web.xml 中的 注册的错误 jsp 中

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

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

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.

剑心龙吟 2024-08-01 13:47:48

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文