过滤器和监听器的基础知识点

前言

上一次碰这一块的知识点已经是两三年前了,后面基本没有用过,再加上以前也没有怎么重视,好吧,我承认我忘完了,毕竟是web三大组件之二,忘了就得让自己再记起来,那就开始了。

过滤器Filter

过滤器其实在很多程序里还是比较常见的,只是我自己没有去弄了解的不太深,下面说一些比较基础的。

什么是Filter

过滤器filter其实是一个接口,我们通常所说的是指实现该接口的相关实现类,它运行在服务器上,工作在通信组件和servlet之间,可以对进入servlet的请求进行过滤,可用于在servlet前后添加相关逻辑,比如记录日志、过滤敏感词等。

过滤器的web.xml配置

filter是需要配置的,他的配置和servlet非常相似,如下所示。

  <filter>
      <filter-name>logFilter</filter-name>
      <filter-class>web.logFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>logFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

当一个请求到servlet需要经过多个过滤器时,执行顺序为filter1–>filter2–>servlet–>filter2–>filter1

filter中的相关方法

public void destroy() {
		//销毁的方法
	}

publicvoid doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
		throws IOException, ServletException {
	//主要方法
	//方法前逻辑
	chain.doFilter(req, res);
	//方法后逻辑
}

public void init(FilterConfig arg0) throws ServletException {
	//初始化的方法
}

filter在tomcat启动时就会实例化并初始化,服务结束时销毁,它是单例的,一个filter的实现类只有一个实例,我们通常写的逻辑都是在doFilter中的chain.doFilter(req, res);前后写。

filter大致就先写这些东西,后面想到什么再来补充。

监听器Listener

监听器其实就真的很少使用了,比如在监听在线人数时可以用到,在这里我就大致先记录一下相关的几个监听器和他们的方法,如果后面有用到再来补充。

什么是Listener

servlet规范中定义的一种特殊的类,作用是监听容器当中产生的一些时间并进行相应的处理。
事件分为两大事件:
一.生命周期相关的事件,指对象的创建与销毁。request,session,servletContext对象时产生的事件
二.绑定事件,指的是当调用request,session,servletContext对象的setAttribute,removeAttribute时产生的事件。即数量的变化。

写一个listener需要实现对应的监听器

web.xml配置

监听器的配置比较简单,只需要一个class就好了。

<listener>
      <listener-class>web.zzzListener</listener-class>
</listener>

相关监听器及方法

ServletContext域
1、生命周期监听接口:ServletContextListener

①contextInitialized(ServletContextEvent sce) 创建ServletContext时调用

②contextDestroyed(ServletContextEvent sce) 销毁ServletContext时调用

2、属性监听接口:ServletContextAttributeListener

①attributeAdded(ServletContextAttributeEvent scae) 添加属性时调用

②attributeRemoved(ServletContextAttributeEvent scae) 移除属性时调用

③attributeReplaced(ServletContextAttributeEvent scae) 修改属性时调用

HttpSession域
3、生命周期监听接口:HttpSessionListener

①sessionCreated(HttpSessionEvent se) 创建session时调用

②sessionDestroyed(HttpSessionEvent se) 销毁session时调用

4、属性监听接口:HttpSessionAttributeListener

①attributeAdded(HttpSessionBindingEvent se) 添加属性时调用

②attributeRemoved(HttpSessionBindingEvent se) 移除属性时调用

③attributeReplaced(HttpSessionBindingEvent se) 修改属性时调用

ServletRequest域
5、生命周期监听接口:HttpSessionListener

①requestInitialized(ServletRequestEvent sre) 创建request时调用

②requestDestroyed(ServletRequestEvent sre) 销毁request时调用

6、属性监听接口:HttpSessionAttributeListener

①attributeAdded(ServletRequestAttributeEvent srae) 添加属性时调用

②attributeRemoved(ServletRequestAttributeEvent srae) 移除属性时调用

③attributeReplaced(ServletRequestAttributeEvent srae) 修改属性时调用

7、感知监听接口:HttpSessionBindingListener

实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和从 Session中删除的事件,不需要在web.xml注册

①valueBound(HttpSessionBindingEvent event) 对象绑定到session时调用

②valueUnbound(HttpSessionBindingEvent event) 对象从session中删除时调用

8、钝化、活化监听器:HttpSessionActivationListener

实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件,不需要在web.xml注册

①sessionDidActivate(HttpSessionEvent se)

②sessionWillPassivate(HttpSessionEvent se)

参考:

https://blog.csdn.net/qq_39164787/article/details/83245166

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值