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

时间:2021-12-10 16:56:11

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可以指定友好的错误页面,比如错误代码为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. How can I do that using the element in the web.xml?

但是,我希望如果用户不满足 中指定的任何错误代码,他或她应该会看到一个默认的错误页面。如何在web.xml中使用元素呢?

3 个解决方案

#1


207  

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. You need to figure which HTTP errors the enduser could possibly face. 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.

但是,在Servlet 2.5中,没有其他方法可以单独指定每个常见的HTTP错误。您需要计算enduser可能面对的HTTP错误。例如,在一个不需要使用HTTP身份验证、禁用目录列表、使用自定义servlet和代码(可能会抛出未处理的异常或没有实现所有方法的代码)的barebones webapp上,您可以分别设置HTTP错误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.

这应该是最常见的。

#2


17  

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页。对于任何其他错误代码,它将返回页面error.html。

#3


1  

You can also specify <error-page> for exceptions using <exception-type>, eg below:

您还可以使用 (如下面的例子)来指定 >。

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

Or map a error code using <error-code>:

或者使用 映射错误代码:

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

#1


207  

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. You need to figure which HTTP errors the enduser could possibly face. 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.

但是,在Servlet 2.5中,没有其他方法可以单独指定每个常见的HTTP错误。您需要计算enduser可能面对的HTTP错误。例如,在一个不需要使用HTTP身份验证、禁用目录列表、使用自定义servlet和代码(可能会抛出未处理的异常或没有实现所有方法的代码)的barebones webapp上,您可以分别设置HTTP错误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.

这应该是最常见的。

#2


17  

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页。对于任何其他错误代码,它将返回页面error.html。

#3


1  

You can also specify <error-page> for exceptions using <exception-type>, eg below:

您还可以使用 (如下面的例子)来指定 >。

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

Or map a error code using <error-code>:

或者使用 映射错误代码:

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