Listener&Filter总结
Listener
监听器(Listener), 监听 某一个事件的发生 或 状态的改变.
监听器的内部机制: 接口回调.
Web监听器的注册
在项目目录/WEB-INF/web.xml
中根节点下注册监听器,语法如下
<listener>
<listener-class>监听器类的完整路径</listener-class>
</listener>
Web监听器的具体分类
Web监听器是一系列接口,共有8个,按监听对象的动作分为3类.
其监听的对象为ServletContext
,Session
和Request
监听三个作用域创建和销毁
用于监听作用域创建与销毁的监听器有以下三个,它们在使用时都需要注册在web.xml
中
ServletContextListener
用于监听ServletContext
的创建与销毁HttpSessionListener
用于监听session
的创建与销毁ServletRequestListener
用于监听Request
的创建与销毁
这三个监听器接口都有xxxInitialized()
和xxxDestroyed()
成员方法,在监听对象创建与销毁时被调用.
三个监听对象的创建时机:
servletcontext
servletcontext
创建时机: 启动服务器时创建servletcontext
销毁时机: 从服务器中移除该项目
Session
Session
创建时机: 调用getSession()
方法会创建Session
.Session
销毁时机: 超时或关闭服务器.
request
:request
创建时机: 客户端访问服务器上的任意资源都会创建requestrequest
销毁时机: 服务器对这次请求做出响应
这三个监听器的作用:
ServletContextListener
:完成初始化工作HttpSessionListener
: 统计在线人数.
监听三个作用域属性状态变更
用于监听作用域属性状态变更的监听器有以下三个,它们在使用时都需要注册在web.xml
中:
ServletContextAttributeListener
用于监听ServletContext
的属性状态变更HttpSessionAttributeListener
用于监听session
的属性状态变更ServletRequestAttributeListener
用于监听Request
的属性状态变更
它们都具有attributeAdded()
,attributeRemoved()
和attributeReplaced()
方法,分别用来监听作用域属性的添加
,删除
和更新
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
//把一个属性存入application范围时触发该方法
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
ServletContext application=event.getServletContext();
//获取添加的属性名和属性值
String name=event.getName();
Object value=event.getValue();
System.out.println(application+"范围内添加了名为"+name+"值为"+value+"的属性");
}
//把一个属性从application范围删除时触发该方法
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
ServletContext application=event.getServletContext();
//获取被删除的属性名和属性值
String name=event.getName();
Object value=event.getValue();
System.out.println(application+"范围内名为"+name+"值为"+value+"的属性被删除了");
}
//替换application范围内的属性时触发该方法
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
ServletContext application=event.getServletContext();
//获取被替换的属性名和属性值
String name=event.getName();
Object value=event.getValue();
System.out.println(application+"范围内"+name+"值为"+value+"的属性被替换了");
}
}
监听httpSession中存值的状态变更
这一类监听器不需要注册,他们实现在Bean
上,监听某一个具体的bean类状态的变化
-
HttpSessionBindingListener
用于监听 对象与session绑定和解绑 的动作,
其valueBound()
方法在对象被绑定时被调用
其valueUnbound()
方法在对象被解除绑定时被调用public class MyBean implements HttpSessionBindingListener { // bean类的成员变量和方法 @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("对象被绑定进来了"); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("对象被解除绑定"); } }
-
HttpSessionActivationListener
监听Session
的值的钝化(序列化)
和活化 (反序列化)
- 钝化与活化
钝化(序列化)
: 把内存中的Session数据存储到硬盘上
活化 (反序列化)
: 把硬盘中的Session数据读取到内存中 - 配置
session
的定时钝化:
通过写入配置文件使Session
对象在超过一定时长就进行钝化,再次访问时活化该Session
在项目目录/WebContent/META-INF/context.xml
中添加如下节点:
<Context> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="钝化定时(以分钟为单位)"> <Store className="org.apache.catalina.session.FileStore" directory="钝化得到文件的存储位置" /> </Manager> </Context>
- 钝化与活化
Filter
过滤器(Filter),拦截客户端发来的请求并进行过滤.
Filter的作用
- 对一些敏感词汇进行过滤
- 统一设置编码
- 自动登录
Filter的注册
在项目目录/WEB-INF/web.xml
中根节点下注册过滤器,其语法如下
<filter>
<filter-name>过滤器类名</filter-name>
<filter-class>过滤器类的完整路径</filter-class>
</filter>
<filter-mapping>
<filter-name>过滤器类名</filter-name>
<url-pattern>过滤器过滤的url,要过滤整个项目则为 /*</url-pattern>
</filter-mapping>
Filter的使用
定义一个Filter
的实现类并注册即可使用该过滤器
public class FilterDemo implements Filter {
// 过滤器初始化时调用此函数
public void init(FilterConfig fConfig) throws ServletException {
// ...
}
// 过滤器销毁时调用此函数
public void destroy() {
// ...
}
// 有请求函数时执行过滤
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 进行过滤
// ...
// 若允许通过过滤器,则执行下一句将请求转发
chain.doFilter(request, response);
// 过滤后执行下面部分
// ...
}
}
Filter的生命周期
Filter的创建时机: 在服务器启动时创建
Filter的销毁时机: 在服务器停止时销毁
Filter执行顺序
- 客户端发出请求的先经过过滤器,若过滤器放行,数据才能到达Servlet.
- 如果有多个过滤器, 那么他们会按照
注册的映射顺序
排队处理. 只要中途有一个过滤器不放行,那么后面排队的过滤器以及目标Servlet都不会收到请求.