本文介绍了如何在Tomcat中的多个错误代码中使用相同的错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从tomcat servlet发送纯文本错误消息,以便应用程序可以将响应呈现给用户.

I'm trying to send plain text error messages from a tomcat servlet, so that the responses may be presented to the user by the application.

我的web.xml中包含以下内容:

I have the following in my web.xml:

<error-page>
    <error-code>409</error-code>
    <location>/string_error.jsp</location>
</error-page>

string_error.jsp如下:

And string_error.jsp is the following:

${requestScope['javax.servlet.error.message']}

这成功为我提供了针对409响应的纯文本错误消息.但是,我想对400/500范围内的任何错误使用同一页面,而无需手动为每个页面指定一个新的<error-page>块.我本以为<error-code>*</error-code>将完成此操作,但事实并非如此. Tomcat是否提供执行此操作的机制?

This successfully gives me plain-text error messages for a 409 response. However, I would like to use this same page for any error in the 400/500 range, without manually specifying a new <error-page> block for each one. I would have assumed that <error-code>*</error-code> would accomplish this, but it does not. Does Tomcat provide a mechanism to do this?

推荐答案

如果您使用的是Servlet 3.0容器(例如Tomcat 7.0),则只需省略<error-code>(或<exception-type>)元素即可它是全局默认错误页面.

If you're using a Servlet 3.0 container, such as Tomcat 7.0, then you can just omit the <error-code> (or <exception-type>) element to make it a global default error page.

<error-page>
    <location>/string_error.jsp</location>
</error-page>

自Servlet 3.0以来,这些元素都是可选的.

Since Servlet 3.0 those elements are namely optional.

但是,如果您尚未使用Servlet 3.0,则必须在容器级别进行配置.例如,在Tomcat 6.0(这是一个Servlet 2.5容器)中,您需要创建一个自定义错误报告Valve类.然后,可以将其指定为 <Host>元素的errorReportValveClass属性/conf/server.xml文件中.

However, if you're not on Servlet 3.0 yet, then you'd have to configure it at the container level. In for example Tomcat 6.0 (which is a Servlet 2.5 container), you'd need to create a custom error report valve class. You can then specify it as errorReportValveClass attribute of the <Host> element in /conf/server.xml file.

对于其他容器,请查阅其文档.

For other containers, consult their documentation.

这篇关于如何在Tomcat中的多个错误代码中使用相同的错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:33