Tomcat 没有重定向到 400 bad request 自定义错误页面 [英] Tomcat is not redirecting to 400 bad request custom error page

查看:343
本文介绍了Tomcat 没有重定向到 400 bad request 自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在生产中有 tomcat 9 Web 服务器.我们面临的问题是,如果 tomcat 收到任何格式错误的 URL,我们希望为我们的应用程序显示自定义错误页面,如下所示

We have tomcat 9 web server in production. The problem we are facing is we want to show the custom error page for our application if tomcat receives any malformed URL as follows

http://URL/|

http://URL/[

我在tomcat应用服务器的web.xml中添加了error page标签如下

I have added error page tag in web.xml of tomcat application server as follows

<error-page>
   <error-code>400</error-code>
   <location>/error.html</location>
 </error-page>
 <error-page>
   <error-code>404</error-code>
   <location>/error.html</location>
 </error-page>

我们在tomcat的webapps目录下的应用程序ROOT文件夹中有error.html.

and we have error.html in our application ROOT folder in webapps directory of tomcat.

每当用户尝试请求任何不存在的网页时,他都会收到与上面指定的 404 错误代码正确对应的错误页面.

Whenever the user tries to request any nonexistent webpage he receives error page correctly corresponding to 404 error code as specified above.

真正的问题是当用户在浏览器中输入格式错误的 URL 时,tomcat 无法重定向到 error.html,例如 http://URL/|

Real Problem is tomcat not able to redirect to error.html when the user enters malformed URL in the browser like http://URL/|

Tomcat 显示的是默认的 400 错误请求错误页面,而不是自定义错误页面.

Tomcat is showing the default 400 bad request error page instead of the custom error page.

推荐答案

这不是错误,而是配置问题.

This is not a bug, it is a configuration issue.

Tomcat 9 改进了错误处理,以便将更多错误,尤其是那些在请求处理开始之前发生的错误传递给标准错误处理机制,而不仅仅是返回错误代码.

Tomcat 9 has improved the error handling so that more errors, particularly those those occur before request processing starts, are passed to the standard error handling mechanism rather than just returning an error code.

对于格式错误的 URI,Tomcat 无法解析 URI,因此 URI 无法映射到 Web 应用程序.因此,Tomcat 无法触发应用程序级别的错误处理,因为它无法识别应用程序.在这种情况下,错误由 ErrorReportValve 处理可以在 server.xml 中配置.

In the case of malformed URIs, it is not possible for Tomcat to parse the URI hence the URI cannot be mapped to a web application. Tomcat is, therefore, unable to trigger the application level error handling because it cannot identify an application. In this case, the error is handled by the ErrorReportValve which can be configured in server.xml.

ErrorReportValve 最近的(ish)新增功能是能够以与每个 Web 应用程序错误页面配置类似的方式为每个状态代码和/或异常类型定义静态网页(即没有 Servlet 或 JSP,只有 HTML).

A recent(ish) addition to the ErrorReportValve is the ability to define static web pages (i.e. no Servlets or JSPs, just HTML) per status code and/or Exception type in a similar manner to the per web application error page configuration.

错误报告阀示例:

<Valve className="org.apache.catalina.valves.ErrorReportValve"
        errorCode.400="webapps/ROOT/error400.html"
        errorCode.0="webapps/ROOT/errorOthers.html"
        showReport="false"
        showServerInfo="false" />

这篇关于Tomcat 没有重定向到 400 bad request 自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆