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>
Servlet过滤器——异常捕获过滤器的更多相关文章
- 在Wireshrak中使用过滤器——捕获过滤器
过滤器可以让你找出你所希望进行分析的数据包.简单来说,一个过滤器就是定义了一定条件,用来包含或者排除数据包的表达式.如果你不希望看到一些数据包,你可以写一恶搞过滤器来屏蔽它们.如果你希望只看到某些数据 ...
- .NET Core整合log4net以及全局异常捕获实现
在使用log4net之前先安装log4net.这里操作很简单,通过nuget下载并安装log4net很方便.如下图. log4net配置 <?xml version="1.0" ...
- 异常捕获设置HTTPStatus
第一步:创建一个异常类 package com.payease.exception; /** * @Created By liuxiaoming * @CreateTime 2017/12/12 下午 ...
- asp.net mvc ,asp.net mvc api 中使用全局过滤器进行异常捕获记录
MVC下的全局异常过滤器注册方式如下:标红为asp.net mvc ,asp.net mvc api 注册全局异常过滤器的不同之处 using SuperManCore; using System. ...
- .NET Core通过过滤器和中间件两种方式实现全局异常捕获和日志记录
1.一共有五类过滤器IAsyncAuthorizationFilter IAsyncResourceFilter IAsyncActonFilter IAsyncExceptionFilter ...
- EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统
EL&Filter&Listener-授课 1 EL表达式和JSTL 1.1 EL表达式 1.1.1 EL表达式介绍 *** EL(Expression Language):表达式语言 ...
- 我的Java之旅 第八课 Servlet 进阶API、过滤器与监听器
1.Servlet.ServletConfig与GenericServlet 首次请求的顺序 => 生成HttpServletRequest与HttpServletRespon ...
- Asp.net MVC3 异常全局过滤器处理
1.建立异常全局过滤器处理机制,在Gloabal.asax.cs文件中,有如下代码块: public static void RegisterGlobalFilters(GlobalFilterCol ...
- Wireshark入门与进阶系列五之常见捕获过滤器
0x00 前言 我们都知道,wireshark可以实现本地抓包,同时Wireshark也支持remote packet capture protocol(rpcapd)协议远程抓包,只要在远程主机上安 ...
随机推荐
- javascript 学习随笔1
html部分 <body onload="message()"><!--主题部分加载就调用-->document.getElementById(" ...
- Gauss elimination Template
Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...
- 使用zxing生成二维码 - servlet形式
因为项目有个功能需要打印二维码,因为我比较喜欢使用html+css+js实现,所以首先想到的是jquery.qrcode.js插件,这个插件可以用canvas和table生成二维码,效果也不错,不过对 ...
- Java --CountDownLatch简介
CountDownLatch 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 coun ...
- IOS Notification 通知中心
1. 通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...
- gethostbyname() -- 用域名或主机名获取IP地址
#include <netdb.h> #include <sys/socket.h> struct hostent *gethostbyname(const char * ...
- Qt核心剖析: 寻找 QObject 的源代码
本来打算把<Qt学习之路>作为一个类似教程的东西,所以就不打算把一些关系到源代码的内容放在那个系列之中啦.因此今天就先来看一个新的开始吧!这个系列估计不会进展很快,因为最近公司里面要做 f ...
- 线程:Message和Runnable
原文地址http://blog.csdn.net/flowingflying/article/details/6370184 程序需要相应用户的操作,最要能在200ms(0.2s)之内,如果超过5秒没 ...
- 基于visual Studio2013解决C语言竞赛题之0903文件读写
题目
- IE6 png图片实现半透明的方法
IE6中支持PNG半透明图片完美解决方法-divcss5亲測 从IE7及IE7以上版本号都支持PNG半透明格式图片,而只有IE6不支持png格式透明图片,而GIF半透明效果不及png半透明格式图片,由 ...