14 监听器(Listener)

  1. 监听器
    1. 监听器概述
      1. 监听器概述

三大组件: Servlet、Filter、Listener

Listener -- 监听器:  (GUI )

> 它是一个接口,内容由我们自己来实现

> 它需要注册,例如注册在按钮上

> 监听器中的方法,会在特殊事件发生时被调用

 

车被偷事件分析:

  • 事件源:车 btn
  • 事件:车被偷 按钮被点击
  • 监听器:警察 listener

监听器中的方法: 实施抓捕 actionPerformed

 

    1. 开发监听器
      1. javaweb中编写监听器
  1. 写一个监听器类: 要求必须实现对应的监听器接口,如:

public class MyXxxListener implements XxxListener{...}

  1. 注册监听: 在web.xml中配置完成注册

<listener>

<listener-class>cn.tedu.listener.MyXxxListener</listener-class>

</listener>

    1. javaweb中的监听器: (八个!)

事件源: 三大域(ServletContext、ServletRequest、HttpSession)

      1. ServletContext(掌握)

(1) 生命周期监听(生死监听):  ServletContextListener,有两个方法, 一个在出生时调用, 一个在死亡时调用

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

void contextDestroyed(ServletContextEvent sce) 销毁ServletContext是调用

(2) 属性监听: ServletContextAttributeListener,有三个方法, 一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用

ServletContext sc = this.getServletContext();

sc.setAttribute(“a”, “xx”);

sc.setAttribute(“a”, “yyy”);

sc.removeAttribute(“a”);

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

void attributeReplaced(ServletContextAttributeEvent scae) 替换属性时调用

void attributeRemoved(ServletContextAttributeEvent scae) 删除属性时调用

      1. HttpSession(了解!!)

(1) 生命周期监听(生死监听): HttpSessionListener,有两个方法,一个在出生时调用, 一个在死亡时调用

void sessionCreated(HttpSessionEvent se) 创建Session时调用

void sessionDestroyed(HttpSessionEvent se) 销毁Session时调用

(2) 属性监听: HttpSessionAttributeListener, 有三个方法,一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用

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

void attributeReplaced(HttpSessionBindingEvent se) 替换属性时调用

void attributeRemoved(HttpSessionBindingEvent se) 删除属性时调用

      1. ServletRequest(了解!!)

(1) 生命周期监听(生死监听):  ServletRequestListener,有两个方法,一个在出生时调用, 一个在死亡时调用

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

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

(2) 属性监听: ServletRequestAttributeListener, 有三个方法,一个在添加属性时调用, 一个在替换属性时调用,一个在移除属性时调用。

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

void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时调用

void attributeRemoved(ServletRequestAttributeEvent srae) 删除属性时调用

 

      1. ServletContextListener监听器案例

监听器案例:重写WEB应用中获取WEB应用虚拟路径的方式。

在WEB应用被加载之后,服务器会立即创建代表当前WEB应用的ServletContext对象, ServletContext对象创建之后激活ServletContextListener监听器,调用 contextInitialized()来进行处理, 在这个方法被调用时, 可以将WEB应用的虚拟路径存入到ServletContext域中, 方便后期在jsp中获取!!

 

在jsp中获取WEB应用的虚拟路径 : ${ pageContext.request.contextPath }  /day17

通过监听器改变获取获取WEB应用的虚拟路径的方式: ${ app } /day17

ServletContext :  

    1. 事件对象(了解)
      1. ServletContextEvent

事件源:ServletContext

事件对象:ServletContextEvent

事件对象提供的方法:getServletContext(); //获取当前事件源

监听器接口:ServletContextListener

      1. HttpSessionEvent

事件源: HttpSession

事件对象: HttpSessionEvent

事件对象提供的方法: getSession(); //获取当前事件源

监听器接口: HttpSessionListener(HttpSession的生命周期监听)

      1. ServletRequestEvent

事件源: ServletRequest

事件对象: ServletRequestEvent

事件对象提供的方法:

getServletRequest(); //获取当前事件源

getServletContext(); //获取代表当前WEB应用的ServletContext对象

监听器接口: ServletRequestListener

      1. ServletContextAttributeEvent

事件源: ServletContex

事件对象: ServletContextAttributeEvent

事件对象提供的方法:

String getName(); //获取属性名

Object getValue(); //获取属性值

getServletContext(); //获取当前事件源

监听器接口: ServletContextAttributeListener

      1. HttpSessionBindingEvent

事件源: HttpSession

事件对象: HttpSessionBindingEvent

事件对象提供的方法:

String getName(); //获取属性名

Object getValue(); //获取属性值

getSession(); //获取当前事件源

监听器接口: HttpSessionAttributeListener

      1. ServletRequestAttributeEvent

事件源: ServletRequest

事件对象: ServletRequestAttributeEvent

事件对象提供的方法:

String getName(); //获取属性名

Object getValue(); //获取属性值

getServletRequest(); //获取当前事件源

getServletContext(); //获取代表当前WEB应用的ServletContext对象

监听器接口: ServletRequestAttributeListener

    1. 使JavaBean感知自己在Session域中状态变化的监听器

* 这两个监听器是用来添加到javaBean上,而不是三大域上!

* 这两个监听器不需要写专门的类去实现,也不需要在web.xml中注册

* 只要让javaBean自己实现监听器接口即可

      1. HttpSessionBindingListener

添加到JavaBean上, JavaBean就知道自己是否被添加到Session中了。

HttpSessionBindingListener提供的方法:

valueBound(HttpSessionBindEvent e); //当前JavaBean感知到自己被添加到Session时调用。

 

valueUnbound(HttpSessionBindEvent e); //当前JavaBean感知到自己被移出Session时调用

 

User user  = new User();

HttpSession session = request.getSession();

session.setAttribute("user", user);

session.removeAttribute(“user”);

      1. HttpSessionActivationListener

添加到JavaBean上,JavaBean就知道自己是随着Session一起钝化了还是活化了

HttpSessionActivationListener提供的方法:

sessionWillPassivate(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起钝化时调用

 

sessionDidActive(HttpSessionEvent e); //当前JavaBean感知自己随着Session一起活化时调用

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月玲珑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值