ipk*_*iss 132 java jsp web.xml servlets custom-error-pages
我<error-page>在web.xml中使用元素来指定用户遇到某个错误时的友好错误页面,例如代码为404的错误:
<error-page>
<error-code>404</error-code>
<location>/Error404.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
但是,我希望如果用户不符合指定的任何错误代码<error-page>,他或她应该看到默认错误页面.如何使用web.xml中的元素执行此操作?
Bal*_*usC 234
在Servlet 3.0或更高版本上,您可以指定
<web-app ...>
<error-page>
<location>/general-error.html</location>
</error-page>
</web-app>
Run Code Online (Sandbox Code Playgroud)
但是当你仍然使用Servlet 2.5时,除了单独指定每个常见的HTTP错误之外别无他法.您需要确定最终用户可能面临的HTTP错误.在一个准系统webapp上,例如使用HTTP身份验证,具有禁用的目录列表,使用自定义servlet和代码可能会抛出未处理的异常或者没有实现所有方法,那么你想为HTTP错误设置它401分别为403,40和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>
Run Code Online (Sandbox Code Playgroud)
这应该包括最常见的.
Guy*_*Guy 22
你也可以这样做:
<error-page>
<error-code>403</error-code>
<location>/403.html</location>
</error-page>
<error-page>
<location>/error.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
对于错误代码403,它将返回页面403.html,对于任何其他错误代码,它将返回页面error.html.
您还可以<error-page>使用<exception-type>例如以下内容指定例外:
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorpages/exception.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
或使用<error-code>以下内容映射错误代码:
<error-page>
<error-code>404</error-code>
<location>/errorpages/404error.html</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
206761 次 |
| 最近记录: |