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>

Servlet过滤器——异常捕获过滤器的更多相关文章

  1. 在Wireshrak中使用过滤器——捕获过滤器

    过滤器可以让你找出你所希望进行分析的数据包.简单来说,一个过滤器就是定义了一定条件,用来包含或者排除数据包的表达式.如果你不希望看到一些数据包,你可以写一恶搞过滤器来屏蔽它们.如果你希望只看到某些数据 ...

  2. .NET Core整合log4net以及全局异常捕获实现

    在使用log4net之前先安装log4net.这里操作很简单,通过nuget下载并安装log4net很方便.如下图. log4net配置 <?xml version="1.0" ...

  3. 异常捕获设置HTTPStatus

    第一步:创建一个异常类 package com.payease.exception; /** * @Created By liuxiaoming * @CreateTime 2017/12/12 下午 ...

  4. asp.net mvc ,asp.net mvc api 中使用全局过滤器进行异常捕获记录

    MVC下的全局异常过滤器注册方式如下:标红为asp.net mvc ,asp.net mvc api  注册全局异常过滤器的不同之处 using SuperManCore; using System. ...

  5. .NET Core通过过滤器和中间件两种方式实现全局异常捕获和日志记录

    1.一共有五类过滤器IAsyncAuthorizationFilter  IAsyncResourceFilter   IAsyncActonFilter  IAsyncExceptionFilter ...

  6. EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统

    EL&Filter&Listener-授课 1 EL表达式和JSTL 1.1 EL表达式 1.1.1 EL表达式介绍 *** EL(Expression Language):表达式语言 ...

  7. 我的Java之旅 第八课 Servlet 进阶API、过滤器与监听器

    1.Servlet.ServletConfig与GenericServlet     首次请求的顺序      => 生成HttpServletRequest与HttpServletRespon ...

  8. Asp.net MVC3 异常全局过滤器处理

    1.建立异常全局过滤器处理机制,在Gloabal.asax.cs文件中,有如下代码块: public static void RegisterGlobalFilters(GlobalFilterCol ...

  9. Wireshark入门与进阶系列五之常见捕获过滤器

    0x00 前言 我们都知道,wireshark可以实现本地抓包,同时Wireshark也支持remote packet capture protocol(rpcapd)协议远程抓包,只要在远程主机上安 ...

随机推荐

  1. web.xml的配置问题

    [转]http://perfy315.iteye.com/blog/2009258 首先 classpath是指 WEB-INF文件夹下的classes目录 ,指的就是java文件编译之后的path. ...

  2. 在DE1-SOC上运行Linux

    1,设定串口终端 安装驱动 :使用mini-USB线将计算机与DE1-SoC的UART转USB接口.drivers\USB2UART_driver文件夹内放置有驱动程序 设定串口终端规格 : 设定串口 ...

  3. 关于RtlInitUnicodeString感想

    01 VOID RtlInitUnicodeString (OUT PUNICODE_STRING DestinationString,IN PCWSTR SourceString OPTIONAL) ...

  4. AWVS介绍(转)

    使用AWVS对域名进行全局分析,深入探索: 首先,介绍一下AWVS这个工具. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网 ...

  5. HDU SPFA算法 Invitation Cards

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 分析: 题意:求1点到其它点的最短距离之和+其它点到1点的最短距离之和 前面一部分直接用SPFA ...

  6. 二维码的妙用:通过Zxing实现wifi账号password分享功能

    二维码是搭载信息的一种载体,通过二维码能够传递名片.网址.商品信息等,本文讲到二维码的第二种妙用:通过二维码实现wifi账号和password分享. 关于二维码的基础知识,请訪问:二维码的生成细节和原 ...

  7. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  8. Activity的创建和使用

    Activity: 1:创建一个类继承Activity或者它的子类 public class MainActivity extends Activity { @Override protected v ...

  9. X窗口系统名词解释

    前端时间Gentoo的桌面环境出了点问题,发现自己对Linux的桌面环境了解的很少,于是恶补了一下知识,以下名词解释基本上都是来自维基百科的条目和<Linux程序设计(第三版)>.一般而言 ...

  10. 网站实战 从效果图开始CSS+DIV 布局华为网站

    经过我们的前面css的学习,我们已经分模块的掌握的CSS的技术,但是,要是完整的做一个页面,我们还没有接触过,这次呢,小强老师来和大家完整的利用CSS+DIV做一个网站案例,我们来模仿下华为的网站. ...