我需要它将多个站点重定向到同一个错误页面 [英] I need that it redirects multiple sites to the same error page
问题描述
我有一个在 Tomcat 上运行的应用程序,它运行良好.当我调用 http://example.com:18080/app 时,它工作正常.现在我创建了一个简单的错误页面来在出现错误时重定向:
I have an application running on Tomcat that is working fine. When I call http://example.com:18080/app it works fine. Now I create simple error page to redirect when there is an error:
<html>
<head>
<title>Error Page</title>
</head>
<body> That's an error! </body>
</html>
我把它添加到我的 web.xml
And I added it on my web.xml
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
当我调用 http://example.com:18080/app/blabla 时转到我的错误页面,但是当我调用 http://example.com:18080/blabla 或只是 http://example.com:18080 我需要它重定向到相同的错误页面.
When I call http://example.com:18080/app/blabla it goes to my error page, but when I call http://example.com:18080/blabla or just http://example.com:18080 I need that it redirects to the same error page.
有人知道怎么做吗?
谢谢
法比奥
推荐答案
Servlet 规范要求错误页面位于 Web 应用程序内部(参见 错误页面).所以你可以:
The Servlet Specification requires for the error pages to be internal to the web application (cf. error pages). So you can either:
- 向所有应用程序添加相同的错误页面,
- 使用特定于容器的机制而不是错误页面.
- add the same error pages to all applications,
- use container specific mechanisms instead of error pages.
在 Tomcat 中,ErrorReportValve
(参见文档) 负责打印应用程序未自定义的错误消息.如果您的错误消息是一个简单的 HTML(比如404.html"),您可以将其保存在任意位置(比如 $CATALINA_BASE/errors
)并添加一个显式的 ErrorReportValve
到 server.xml
:
In Tomcat the ErrorReportValve
(cf. documentation) is responsible for printing the error messages which are not customized by the application. If your error message is a simple HTML (let's say "404.html") you can save it in an arbitrary location (let's say $CATALINA_BASE/errors
) and add an explicit ErrorReportValve
to server.xml
:
<Host>
...
<Valve
className="org.apache.catalina.valves.ErrorReportValve"
showServerInfo="false"
errorCode.404="errors/404.html"/>
</Host>
但是,如果要使用 JSP 或 servlet 来格式化错误页面,则需要:
However, if you want to use a JSP or servlet to format the error page, you need to:
- 通过将以下内容添加到
$CATALINA_BASE/conf/web.xml
,将
元素添加到所有应用程序:
- Add the
<error-page>
element to all applications by adding the following to$CATALINA_BASE/conf/web.xml
:
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errors/404.jsp</location>
</error-page>
- 在所有应用程序的
WEB-INF/errors
下安装$CATALINA_BASE/errors
文件夹.这可以通过将以下内容添加到$CATALINA_BASE/conf/context.xml
中的
元素来完成:
- Mount the
$CATALINA_BASE/errors
folder underWEB-INF/errors
in all applications. This can by done by adding the following to the<Context>
element in the$CATALINA_BASE/conf/context.xml
:
<Context>
...
<Resources
className="org.apache.catalina.webresources.StandardRoot">
<PostResources
className="org.apache.catalina.webresources.DirResourceSet"
base="${catalina.base}/errors"
webAppMount="/WEB-INF/errors"/>
</Resources>
</Context>
这篇关于我需要它将多个站点重定向到同一个错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!