监听器

为什么要使用监听器?

在 Servlet中学习 了 request、session、 application 作用域对象,其主要作用是实现数据的在不同场景中的灵活流转。但是数据的具体流转过程是看不到的,比如作用域对象是什么时候创建和销毁的,数据是什么时候存取,改变和删除的。因为具体的流转过程看不到,所以也就无法再指定的时机对数据和对象进行操作

监听器的概念

Servlet 监听器是 Servlet 规范中定义的一种特殊类,用 于监听 ServletContext、HttpSession 和 ServletRequest 等域对象的创建与销毁事件,以及监听这些域对象中属性发生修改的事件

监听对象

Request Session Application

监听内容

创建、销毁、属性改变事件

监听作用

在事件发生之前或者之后进行一些处理,比如统计在线人数

使用

监听作用域对象request、session、application的创建、销毁和内容的改变

1. 创建一个实现了指定接口的java类

监听request——实现ServletRequestListener接口

作用:监听request对象的创建和销毁
方法:
requestInitialized(ServletRequestEvent sre)——返回这次请求创建的req对象
requestDestroyed(ServletRequestEvent sre)——返回这次请求销毁的req对象
注:
形参可以获取监听的request对象
sre.getServletRequest();

监听request——实现ServletRequestAttributeListener接口

作用:监听request作用域数据的变更
方法:
attributeAdded(ServletRequestAttributeEvent srae)
attributeRemoved(ServletRequestAttributeEvent srae)
attributeReplaced(ServletRequestAttributeEvent srae)
注:形参可以获取被监听的数据
srae.getName() 获取监听数据的键
srae.getValue() 获取监听数据的值

监听session——实现HttpSessionListener接口

作用:监听session的创建和销毁
方法:
sessionCreated(HttpSessionEvent se) 创建
sessionDestroyed(HttpSessionEvent se) 销毁
注:形参可以获取被监听的session对象
se.getSession();

监听session——实现HttpSessionAttributeListener接口

作用:监听session数据的变更
方法:
attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)
注:形参可以获取被监听的数据
event.getName() 获取数据的键名
event.getValue() 获取数据的值

监听application——实现ServletContextListener接口

作用:监听application对象的初始化和销毁
方法:
contextInitialized(ServletContextEvent sce) 初始化 服务器启动
contextDestroyed(ServletContextEvent sce) 销毁 服务器关闭
注:
形参可以获取当前application对象
sce.getServletContext();

监听application——实现ServletContextAttributeListener接口

作用:监听数据的变更
方法:
attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)
注:
形参可以获取当前监听的数据
event.getName() 获取数据的键名
event.getValue() 获取数据的值


public class MyListener implements ServletRequestListener,
ServletRequestAttributeListener,
HttpSessionListener,HttpSessionAttributeListener,
ServletContextListener,ServletContextAttributeListener{
	//request对象销毁
	public void requestDestroyed(ServletRequestEvent sre) {
		System.out.println("我被销毁了");
		
	}
	//request对象创建
	public void requestInitialized(ServletRequestEvent sre) {
		System.out.println("我被创建了");
		
	}
	//监听request作用域数据的添加

	public void attributeAdded(ServletRequestAttributeEvent srae) {
		
		System.out.println("request中增加了一条数据-"+srae.getName()+":"+srae.getValue());
		
	}

	public void attributeRemoved(ServletRequestAttributeEvent srae) {
		// TODO Auto-generated method stub
		
	}

	public void attributeReplaced(ServletRequestAttributeEvent srae) {

		
	}
/*------------------------------------------------------------------------------*/
	//监听session的创建

	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("session被创建了");
		
	}
	//监听session的销毁

	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("session被销毁了");
		
	}
	//监听session数据的表更

	public void attributeAdded(HttpSessionBindingEvent event) {
		System.out.println("session中增加了一条数据"+event.getName()+":"+event.getValue());
	}

	public void attributeRemoved(HttpSessionBindingEvent event) {

		
	}

	public void attributeReplaced(HttpSessionBindingEvent event) {

		
	}
/*------------------------------------------------------------------------------*/

	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("application对象被初始化了");
		
	}
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("application对象被销毁了");
		
	}
	//监听application的数据变更
	public void attributeAdded(ServletContextAttributeEvent event) {
		System.out.println("application中存储了数据:"+event.getName()+":"+event.getValue());
		
	}

	public void attributeRemoved(ServletContextAttributeEvent event) {

	}

	public void attributeReplaced(ServletContextAttributeEvent event) {
	
		
	}
		

2. 在web.xml中配置监听器类

<listener>
		<listener-class>com.bjsxt.listener.MyListener</listener-class>
</listener>

事例

  1. 统计当前在线人数
  2. 统计网页浏览器次数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值