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>

 

转载于:https://www.cnblogs.com/zkn11199/p/5600340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用servlet过滤器可以完成具体的业务功能。servlet过滤器servlet规范中的一种特殊组件,用于在请求到达目标servlet之前,对请求进行预处理或后处理。 首先,我们可以通过servlet过滤器来实现用户认证的功能。在用户发送请求时,通过过滤器对用户进行身份验证,比如检查用户的登录状态或者验证用户的身份证明。如果用户认证失败,则可以拦截请求并返回相应的错误信息。 其次,我们可以利用servlet过滤器来处理请求的编码和解码。在网络传输中,数据的编码和解码是必要的,通过过滤器我们可以对请求和响应的数据进行编码和解码操作,确保数据的正确传输。 另外,servlet过滤器还可以用于日志记录和统计功能。我们可以通过过滤器来记录用户的访问日志,包括访问时间、IP地址等信息。通过对这些日志进行分析,可以提供一些统计报表,如最受欢迎的页面、访问量最高的时间段等,有利于进行业务分析和优化。 此外,servlet过滤器还可以用于请求的错误处理。当一个请求发生错误时,过滤器可以捕获异常并对错误进行处理,比如自定义错误页面或提供友好的错误提示信息。 综上所述,通过使用servlet过滤器,我们可以完成具体的业务功能,包括用户认证、请求的编码和解码、日志记录和统计以及错误处理等。通过对请求进行预处理或后处理,可以更好地保护和管理我们的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值