Servlet过滤器——异常捕获过滤器

1.概述

   介绍如何实现异常捕获过滤器。

 

2.技术要点

   本实例主要是在过滤器Filter的doFilter()方法中,对执行过滤器链的chain的doFilter()语句处添加try…catch异常捕获语句,然后在chach语句中,循环异常对象,直到找出根异常为止。

 

3.具体实现

(1)创建Filter实现类ExceptionFilter.java,利用throwable抛出异常,去捕捉异常原因并转到相应的页面中主要代码为:

public class ExceptionFilter implements Filter {
      public void destroy() {
      }
      public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
            try {
                  chain.doFilter(request, response);
            } catch (Exception e) {   //如果有异常则捕捉
                  Throwable rootCause = e;
                  while (rootCause.getCause() != null) {
                        rootCause = rootCause.getCause();
                  }
                  String errormessage = rootCause.getMessage();  //异常根本
                  errormessage = errormessage == null ? "异常:" + rootCause.getClass().getName()
                              : errormessage;                                       //中止传递异常的原因
                  request.setAttribute("errormessage", errormessage); 
                  request.setAttribute("e", e);
                  if (rootCause instanceof LoginException) {                                //1转到登录页面
                        request.getRequestDispatcher("/LoginException.jsp").forward(
                                    request, response);
                  } else if (rootCause instanceof OperationException) {
                                                                                    //2转到操作页面
                        request.getRequestDispatcher("/OperationException.jsp").forward(                                    request, response);
                  } else {
                        request.getRequestDispatcher("/exception.jsp").forward(request,   //其它异常
                                    response);
                  }
            }
      }
      public void init(FilterConfig arg0) throws ServletException {
      }
}

(2)创建LoginException.jsp登录异常页面主要代码为:

<div align="center" style="font-size: large;">后台操作</div>
<form action="">
      <table align="center">
            <tr>
                  <td>帐号</td>
                  <td><input type="text" name="account" /></td>
            </tr>
            <tr>
                  <td>密码</td>
                  <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                  <td align="center" colspan="2"><input type="submit" value=" 登录 " />
                  <input type="submit" value="退出"/></td>
            </tr>
      </table>
</form>
<div class="error" align="center">
${ errormessage } 
</div>

(3)创建OperationException操作异常页面主要代码如下:

<div class="error" align="center">
${ errormessage } <a href="javascript:history.go(-1); ">返回上一级操作</a>
</div>

(4)创建Exceptionmain.jsp页面,关键代码如下:

<%
      String action = request.getParameter("action");
      if("OperationException".equals(action)){
            throw new OperationException("此操作失败.  ");
      }
      else if("LoginException".equals(action)){
            throw new LoginException("请您登陆后再进行此项操作.  ");
      }
      else if("exception".equals(action)){
            Integer.parseInt("mull空传递参数");
      }
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>异常捕捉过滤器</title>
</head>
<body>
<table align="left" cellpadding="2" cellspacing="2">
<Tr><td><a href="${ pageContext.request.requestURI }?action=OperationException">过滤操作</a></td></Tr>
<tr><td><a href="${ pageContext.request.requestURI }?action=LoginException">过滤登录</a></td></tr>
<Tr><td><a href="${ pageContext.request.requestURI }?action=exception">过滤异常</a></td></Tr>
</table>

 

posted @ 2016-06-21 16:29  柯南&  阅读(3834)  评论(0编辑  收藏  举报