简体   繁体   English

如何在 web.xml 中指定默认错误页面?

[英]How to specify the default error page in web.xml?

I am using <error-page> element in web.xml to specify the friendly error page when user encounters a certain error such as error with code of 404:我在web.xml中使用<error-page>元素来指定当用户遇到特定错误时的友好错误页面,例如代码为 404 的错误:

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

However, I want that if the user does not meet any error code specified in <error-page> , he or she should see a default error page.但是,我希望如果用户没有遇到<error-page>中指定的任何错误代码,他或她应该会看到默认错误页面。 How can I do that using the element in the web.xml ?如何使用web.xml中的元素来做到这一点?

On Servlet 3.0 or newer you could just specify在 Servlet 3.0 或更高版本上,您可以指定

<web-app ...>
    <error-page>
        <location>/general-error.html</location>
    </error-page>
</web-app>

But as you're still on Servlet 2.5, there's no other way than specifying every common HTTP error individually.但由于您仍在使用 Servlet 2.5,除了单独指定每个常见的 HTTP 错误之外别无他法。 You need to figure which HTTP errors the enduser could possibly face.您需要弄清楚最终用户可能面临哪些 HTTP 错误。 On a barebones webapp with for example the usage of HTTP authentication, having a disabled directory listing, using custom servlets and code which can possibly throw unhandled exceptions or does not have all methods implemented, then you'd like to set it for HTTP errors 401, 403, 500 and 503 respectively. On a barebones webapp with for example the usage of HTTP authentication, having a disabled directory listing, using custom servlets and code which can possibly throw unhandled exceptions or does not have all methods implemented, then you'd like to set it for HTTP errors 401 , 403, 500 和 503。

<error-page>
    <!-- Missing login -->
    <error-code>401</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Forbidden directory listing -->
    <error-code>403</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Missing resource -->
    <error-code>404</error-code>
    <location>/Error404.html</location>
</error-page>
<error-page>
    <!-- Uncaught exception -->
    <error-code>500</error-code>
    <location>/general-error.html</location>
</error-page>
<error-page>
    <!-- Unsupported servlet method -->
    <error-code>503</error-code>
    <location>/general-error.html</location>
</error-page>

That should cover the most common ones.这应该涵盖最常见的那些。

You can also do something like that:你也可以这样做:

<error-page>
    <error-code>403</error-code>
    <location>/403.html</location>
</error-page>

<error-page>
    <location>/error.html</location>
</error-page>

For error code 403 it will return the page 403.html, and for any other error code it will return the page error.html.对于错误代码 403,它将返回页面 403.html,对于任何其他错误代码,它将返回页面 error.html。

You can also specify <error-page> for exceptions using <exception-type> , eg below:您还可以使用<exception-type>为异常指定<error-page> ,例如:

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

Or map a error code using <error-code> :或 map 使用<error-code>的错误代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM