web组件之Listener

1.Listener

[1]监听器简介

监听器是JavaWeb中三大组件之一(Servlet、Filter、Listener)

JavaWeb的组件的共同特点:

  • 都需要实现某个接口

  • 都需要在web.xml文件中进行注册

朝阳群众实际上就是一个监听器。

  • 监听器:朝阳群众

  • 监听对象:明星

  • 事件:干坏事

  • 行为:报警

学习监听器要先搞清楚监听器是监听谁的?

JavaWeb的监听器主要是监听域对象(ServletRequest、HttpSession、ServletContext)

[2]监听器分类

监听器一共有三组,共八个。

  • 生命周期变化监听器

监听ServletRequest、HttpSession、ServletContext的创建和销毁

ServletContextListener

ServletRequestListener

HttpSessionListener

  • 属性变化监听器

监听ServletRequest、HttpSession、ServletContext的属性的变化

ServletContextAttributeListener

HttpSessionAttributeListener

ServletRequestAttributeListener

  • javaBean对象的在Session中添加和钝化的监听器

HttpSessionBindingListener

HttpSessionActivationListener

[3]生命周期监听器

  • 主要监听三个对象的创建和销毁的

ServletContextListener

  • ServletContext对象会在项目启动时创建,在项目卸载时销毁。

  • 方法:

void contextDestroyed(ServletContextEvent sce)

  • 这个方法会在ServletContext销毁前调用

void contextInitialized(ServletContextEvent sce)

  • 这个方法会在ServletContext创建之后调用

ServletContextEvent

-方法:

ServletContext getServletContext ()

可以获取到ServletContext对象

  • 使用步骤

1.创建一个类实现ServletContextListener接口

2.在web.xml文件进行配置

< listener>

< listener-class>监听器的全类名< /listener-class>

< /listener>

HttpSessionListener

  • 方法:

void sessionCreated(HttpSessionEvent se)

  • session创建之后调用

void sessionDestroyed(HttpSessionEvent se)

  • session对象销毁前调用

HttpSessionEvent

  • 作用:

HttpSession getSession ()

可以获取到当前Session对象。

ServletRequestListener

  • 方法:

void requestDestroyed(ServletRequestEvent sre)

  • Request对象销毁前调用

void requestInitialized(ServletRequestEvent sre)

  • Request对象创建之后调用

ServletRequestEvent

作用:

ServletRequest getServletRequest ()

可以获取当前的request对象

ServletContext getServletContext ()

可以获取ServletContext对象

====================================================

[4]属性监听器

监听三个对象中的属性变化

4- ServletContextAttributeListener

void attributeAdded(ServletContextAttributeEvent scab)

  • 当向ServletContext中添加属性时,调用该方法

void attributeRemoved(ServletContextAttributeEvent scab)

  • 当从ServletContext中移除一个属性时,调用该方法

void attributeReplaced(ServletContextAttributeEvent scab)

  • 当ServletContext中的一个属性被替换时,调用该方法

ServletContextAttributeEvent

  • String getName() –> 获取属性名

  • Object getValue() –> 获取属性值

  • 注意当替换属性值,获取到的是旧值,即被替换的值。

  • ServletContext getServletContext() –> 获取ServletContext对象

5- HttpSessionAttributeListener

void attributeAdded(HttpSessionBindingEvent se)

  • 向session中添加属性时调用

void attributeRemoved(HttpSessionBindingEvent se)

  • 从session中移除属性时调用

void attributeReplaced(HttpSessionBindingEvent se)

  • 替换session中属性时调用

HttpSessionBindingEvent

  • String getName() –> 获取属性名

  • Object getValue() –> 获取属性值

  • 注意当替换属性值,获取到的是旧值

  • HttpSession getSession() –> 获取session对象

6- ServletRequestAttributeListener

void attributeAdded(ServletRequestAttributeEvent srae)

  • 向request中添加属性时调用

void attributeRemoved(ServletRequestAttributeEvent srae)

  • 从request中移除属性时调用

void attributeReplaced(ServletRequestAttributeEvent srae)

  • 替换request中属性时调用

ServletRequestAttributeEvent

  • String getName() –> 获取属性名

  • Object getValue() –> 获取属性值

  • 注意当替换属性值,获取到的是旧值

  • ServletRequest getServletRequest ()

可以获取当前的request对象

  • ServletContext getServletContext ()

可以获取ServletContext对象

以下两个监听器不需要创建一个专门的类来实现,而是由我们的JavaBean来实现

同样也不需要再web.xml文件进行配置

这两个监听器主要是监听某个JavaBean对象在Session中的的变化

[7]HttpSessionBindingListener

该监听器主要监听某个javabean的实例在session中添加和移除

void valueBound(HttpSessionBindingEvent event)

  • 当该类的实例设置进session对象时调用

void valueUnbound(HttpSessionBindingEvent event)

  • 当该类的实例从session中移除时调用

HttpSessionBindingEvent

  • String getName() –> 获取属性名

  • Object getValue() –> 获取属性值

  • 注意当替换属性值,获取到的是旧值

  • HttpSession getSession() –> 获取session对象

[8]HttpSessionActivationListener

监听某个javaBean的实例是否和Session一起活化或钝化

  • 活化:将硬盘中的对象反序列化内存中

  • 钝化:将内存中的对象序列化到硬盘中

void sessionWillPassivate(HttpSessionEvent se)

  • 当该类的实例和session一起钝化到硬盘时调用

void sessionDidActivate(HttpSessionEvent se)

  • 当该类的实例和session一起活化到内存时调用

HttpSessionEvent

  • 作用:

HttpSession getSession ()

可以获取到当前Session对象。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值