【问题标题】:How to specify common error page excluding 404 and 500 in web.xml如何在 web.xml 中指定不包括 404 和 500 的常见错误页面
【发布时间】:2017-11-16 14:40:08
【问题描述】:
我已经在 web.xml 中有两个为 404 和 500 错误页面设计的网页
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error/500.jsp</location>
</error-page>
我想针对所有其他错误重定向到预先指定的页面。
请帮忙。
谢谢
【问题讨论】:
标签:
java
error-handling
web.xml
【解决方案1】:
这应该足够了,再加上一些与客户错误相关的页面,您可以添加默认页面。所以除了 404, 500 之外的所有错误都会被重定向到通用页面error.html
<error-page>
<location>/error.html</location>
</error-page>
或更多自定义,使用 Exception 类,也可以是您的自定义异常类。
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.html</location>
</error-page>
所以代码变成了,
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/error/500.jsp</location>
</error-page>
<error-page>
<location>/error.html</location>
</error-page>