Listener&Filter总结

Listener

监听器(Listener), 监听 某一个事件的发生 或 状态的改变.
监听器的内部机制: 接口回调.

Web监听器的注册

项目目录/WEB-INF/web.xml中根节点下注册监听器,语法如下

<listener>
	<listener-class>监听器类的完整路径</listener-class>
</listener>

Web监听器的具体分类

Web监听器是一系列接口,共有8个,按监听对象的动作分为3类.
其监听的对象为ServletContext,SessionRequest

监听三个作用域创建和销毁

用于监听作用域创建与销毁的监听器有以下三个,它们在使用时都需要注册在web.xml

  1. ServletContextListener用于监听ServletContext的创建与销毁
  2. HttpSessionListener用于监听session的创建与销毁
  3. ServletRequestListener用于监听Request的创建与销毁

这三个监听器接口都有xxxInitialized()xxxDestroyed()成员方法,在监听对象创建与销毁时被调用.
三个监听对象的创建时机:

  1. servletcontext
    1. servletcontext创建时机: 启动服务器时创建
    2. servletcontext销毁时机: 从服务器中移除该项目
  2. Session
    1. Session创建时机: 调用getSession()方法会创建Session.
    2. Session销毁时机: 超时或关闭服务器.
  3. request:
    1. request创建时机: 客户端访问服务器上的任意资源都会创建request
    2. request销毁时机: 服务器对这次请求做出响应

这三个监听器的作用:

  1. ServletContextListener:完成初始化工作
  2. HttpSessionListener: 统计在线人数.

监听三个作用域属性状态变更

用于监听作用域属性状态变更的监听器有以下三个,它们在使用时都需要注册在web.xml中:

  1. ServletContextAttributeListener用于监听ServletContext的属性状态变更
  2. HttpSessionAttributeListener用于监听session的属性状态变更
  3. 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类状态的变化

  1. 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("对象被解除绑定");
    	}
    
    }
    
  2. HttpSessionActivationListener监听Session的值的 钝化(序列化)活化 (反序列化)

    1. 钝化与活化
      钝化(序列化): 把内存中的Session数据存储到硬盘上
      活化 (反序列化): 把硬盘中的Session数据读取到内存中
    2. 配置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的作用

  1. 对一些敏感词汇进行过滤
  2. 统一设置编码
  3. 自动登录

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执行顺序

  1. 客户端发出请求的先经过过滤器,若过滤器放行,数据才能到达Servlet.
  2. 如果有多个过滤器, 那么他们会按照注册的映射顺序排队处理. 只要中途有一个过滤器不放行,那么后面排队的过滤器以及目标Servlet都不会收到请求.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值