我们在开发java web网站应用的时候,经常会遇到后台异常出错,或者用户访问的url不存在。或者后台出现的比较特别的异常处理。如今,我们可以在web.xml中对于这些错误进行特殊的跳转提示。例如你的servlet出现异常,你就可以跳转到servlet错误提示页面。但是你的url不存在,你就可以跳转到404的错误页面。等等的情况。
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置错误处理 --> <error-page> <error-code>404</error-code> <location>/404.html</location> </error-page> <error-page> <error-code>505</error-code> <location>/505.html</location> </error-page> <!-- 配置异常处理 --> <error-page> <exception-type>javax.servle.ServletException</exception-type> <location>/error.html</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.html</location> </error-page> </web-app>
异常:
当访问一个action或是servlet的时,可能会出现异常,
struts2中web.xml配置:
<default-action-ref name="pagenotfound"></default-action-ref>? <action name="pagenotfound"> <result>/pagenotfound.html</result>? </action>
servlet中web.xml配置:
<error-page> <exception-type>javax.servle.ServletException</exception-type> <location>/error.html</location> </error-page>
-----------------------------------------------------
转载请注明来源此处
原地址:#
发表