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

查看:176
本文介绍了如何在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>

但是,如果用户不符合< 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错误。在一个准系统webapp上,例如使用HTTP身份验证,具有禁用的目录列表,使用自定义servlet和代码可能会抛出未处理的异常或者没有实现所有方法,那么你想为HTTP错误设置它401分别为403,300和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天全站免登陆