在异常处理机制中,有一种处理机制叫做声明式异常,声明式异常,就是系统发生错误时,自动跳转到相应的业务,而不需要手动去捕捉处理,这样错误的处理方式比较简单,但是灵活性较差。
使用声明式异常,必须xml文件中标明isErrorPage="true"。
<error-page> <exception-type>com.cx.drp.util.ApplicationException</exception-type> <location>/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/http_error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/http_error.jsp</location> </error-page>
之所以采用http_error.jsp的原因是因为IE不能很好的直接解析错误页,需要通过手动配置跳转。
<body> <% Integer errorCode=(Integer)request.getAttribute("javax.servlet.error.status_code"); if(errorCode==404){ response.sendRedirect(request.getContextPath()+"/404.jsp"); }else if(errorCode==500){ response.sendRedirect(request.getContextPath()+"500.jsp"); } %> </body>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312" /> </head> <body> 未找到请求的页面 </body> </html>