如何在 web.xml 中指定默认错误页面? [英] How to specify the default error page in web.xml?

查看:46
本文介绍了如何在 web.xml 中指定默认错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.xml中使用<error-page>元素来指定当用户遇到某些错误时友好的错误页面,例如代码为404的错误:

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:

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

但是,我希望如果用户没有遇到 中指定的任何错误代码,他或她应该看到一个默认的错误页面.我如何使用 web.xml 中的元素来做到这一点?

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?

推荐答案

在 Servlet 3.0 或更新版本上,您可以指定

On Servlet 3.0 or newer you could just specify

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

但由于您仍在使用 Servlet 2.5,因此除了单独指定每个常见的 HTTP 错误之外别无他法.您需要确定最终用户可能面临哪些 HTTP 错误.例如,在使用 HTTP 身份验证、禁用目录列表、使用自定义 servlet 和可能抛出未处理异常或未实现所有方法的代码的准系统 Web 应用程序上,您希望将其设置为 HTTP 错误 401、403、500 和 503.

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.

<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>

这应该涵盖最常见的.

这篇关于如何在 web.xml 中指定默认错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆