• 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>
  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/zkn11199/p/5600340.html
Copyright © 2020-2023  润新知