java 如何从 web.xml 捕获到同一页面的所有错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2956167/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:40:37  来源:igfitidea点击:

How can I catch all errors to same page from web.xml?

javaweb.xmlcustom-error-pagesdeployment-descriptor

提问by newbie

I tried to use

我试着用

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errors/error.jsp</location>  
</error-page> 

but i dosen't catch 404 errors. How can I catch also 404 etc. errors to that same page ? I want to catch ALL error codes to same error page jsp.

但我没有发现 404 错误。我怎样才能捕捉到同一页面的 404 等错误?我想将所有错误代码捕获到相同的错误页面 jsp。

采纳答案by JoseK

You can add an <error-code>tag for that

您可以<error-code>为此添加标签

<error-page>
    <error-code>404</error-code>
    <location>/errors/error.jsp</location>
</error-page> 

UPDATE:

更新:

As per your updated question - you'll have to define EACH error-code individually in the web.xml.

根据您更新的问题 - 您必须在 web.xml 中单独定义每个错误代码。

Using <exception-type>java.lang.Throwable</exception-type>will catch the error 500s but not 404s

使用<exception-type>java.lang.Throwable</exception-type>将捕获错误 500s 但不会捕获 404s

回答by Mircea Stanciu

I am using this:

我正在使用这个:

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/pages/exception/sorry.html</location>
</error-page>

<error-page>
    <error-code>403</error-code>
    <location>/security/403</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/security/404</location>
</error-page>

回答by ozma

In Tomcat7 (might work on older versions but I didn't check)

在 Tomcat7 中(可能适用于旧版本,但我没有检查)

add the error pages you want (e.g 404.jsp, general_error.php etc.)

添加您想要的错误页面(例如 404.jsp、general_error.php 等)

add to web.xml (all first and then specific. adapt it to your code of course):

添加到 web.xml (首先,然后是特定的。当然要根据您的代码进行调整):

<error-page>
    <location>general_error.php</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>404.jsp</location>
</error-page>

<error-page>
    <error-code>409</error-code>
    <location>error_page.jsp?error=custom_message</location>
</error-page>