servlet声明式异常编号处理

 在异常处理机制中,有一种处理机制叫做声明式异常,声明式异常,就是系统发生错误时,自动跳转到相应的业务,而不需要手动去捕捉处理,这样错误的处理方式比较简单,但是灵活性较差。

 

  使用声明式异常,必须xml文件中标明isErrorPage="true"。

  

Web.xml配置文件代码 

 收藏代码

  1. <error-page>  
  2.         <exception-type>com.cx.drp.util.ApplicationException</exception-type>  
  3.         <location>/error.jsp</location>  
  4.     </error-page>  
  5.       
  6.     <error-page>  
  7.         <error-code>404</error-code>  
  8.         <location>/http_error.jsp</location>  
  9.     </error-page>  
  10.       
  11.     <error-page>  
  12.         <error-code>500</error-code>  
  13.         <location>/http_error.jsp</location>  
  14.     </error-page>  

 之所以采用http_error.jsp的原因是因为IE不能很好的直接解析错误页,需要通过手动配置跳转。

Http_error.jsp代码 

 收藏代码

  1. <body>  
  2.    <%  
  3.     Integer errorCode=(Integer)request.getAttribute("javax.servlet.error.status_code");  
  4.     if(errorCode==404){  
  5.         response.sendRedirect(request.getContextPath()+"/404.jsp");  
  6.     }else if(errorCode==500){  
  7.         response.sendRedirect(request.getContextPath()+"500.jsp");  
  8.     }  
  9.     %>  
  10.   </body>  

 

404.jsp代码 

 收藏代码

  1. <%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3.   
  4. <html>  
  5.     <head>  
  6.         <meta http-equiv="content-type" content="text/html;charset=gb2312" />  
  7.     </head>  
  8.     <body>  
  9.         未找到请求的页面  
  10.     </body>  
  11. </html>  

猜你喜欢

转载自blog.csdn.net/jdnicky/article/details/91038026