java 多重错误码配置web.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2237434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiple error-code configuration web.xml
提问by coder247
I'd like to direct all errors to my Errorsevlet without specifying all the codes explicitly. Is there any way to do like that?
我想将所有错误定向到我的 Errorsevlet 而不明确指定所有代码。有没有办法做到这一点?
<error-page>
<error-code>400</error-code>
<location>/servlet/com.abc.servlet.ErrorServlet</location>
</error-page>
**And after reaching the ErrorServlet how can i get the stack trace of the error in the servlet. So that i can email the details when one error occurs. **
**在到达 ErrorServlet 之后,我如何获得 servlet 中错误的堆栈跟踪。这样我就可以在发生错误时通过电子邮件发送详细信息。**
采纳答案by Teja Kantamneni
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/servlet/com.ibm.eisa.servlet.ErrorServlet</location>
</error-page>
Try this one, all of your errors will be caught(500's) not 404 etc
试试这个,你所有的错误都会被捕获(500's)而不是 404 等
回答by BalusC
If you can upgrade, since Servlet 3.0 it's possible to have a generic error page for allerrors, even those not caused by an exception (e.g. 404, 401, etc). Just omit the <error-code>or <exception-type>altogether so that you only have a <location>.
如果可以升级,从 Servlet 3.0 开始,所有错误都可能有一个通用错误页面,即使是那些不是由异常引起的错误(例如 404、401 等)。只需省略<error-code>or<exception-type>完全这样你就只有一个<location>.
<error-page>
<location>/errorServlet</location>
</error-page>
Note that I replaced the URL to avoid the use of Tomcat's builtin and deprecated InvokerServlet.
请注意,我替换了 URL 以避免使用 Tomcat 的内置和不推荐使用的InvokerServlet.
回答by lucrussell
You will need to specify all the desired codes explicitly, a wildcard mechanism is not supported. There are not that many codes, here is a full list.
您需要明确指定所有所需的代码,不支持通配符机制。没有那么多代码,这里是一个完整的列表。
To print out the stacktrace (e.g. in a comment, for debugging purposes), you could do something like this:
要打印堆栈跟踪(例如,在注释中,出于调试目的),您可以执行以下操作:
<%@ page isErrorPage="true" import="java.io.*"%>
<body>
<p>Sorry, there was an error.</p>
<!-- The full stacktrace follows:-->
<!--
<%
if (exception != null) {
exception.printStackTrace(new PrintWriter(out));
}
%>
-->
</body>
回答by Peter
I had same concern and after some research I have found out that unfortunately there is no clear requirement to support default error page in Servlet 3.0 specs.
我有同样的担忧,经过一些研究,我发现不幸的是,在 Servlet 3.0 规范中没有明确要求支持默认错误页面。
It's misleading that "error-code" or "exception-type" are optional tags in XSD so we tend to consider that default error page will be the one without "error-code" and without "exception-type" tag.
“错误代码”或“异常类型”是 XSD 中的可选标签是一种误导,因此我们倾向于认为默认错误页面将是没有“错误代码”和“异常类型”标签的页面。
Some application servers (e.g. GlassFish) behave as we wish, take default error page, then following the order of specific error pages they override default error page.
一些应用程序服务器(例如 GlassFish)按照我们的意愿运行,采用默认错误页面,然后按照特定错误页面的顺序覆盖默认错误页面。
I also tested this on WebLogic 12c and I couldn't get it working as on GlassFish. Below article gives more clues about Tomcat.
我还在 WebLogic 12c 上对此进行了测试,但无法像在 GlassFish 上那样工作。下面的文章提供了更多关于 Tomcat 的线索。
See: bz.apache.org/bugzilla/show_bug.cgi?id=52135
参见:bz.apache.org/bugzilla/show_bug.cgi?id=52135

