2021-11-16 Listener 监听器的学习

一、 Listener 按照监听的对象分类共有六种

  • ServletContext 对象生命周期监听器与属性操作监听器;
  • HttpSession 对象生命周期监听器与属性操作监听器;
  • ServletRequest 对象生命周期监听器与属性操作监听器;

二、细分

1.ServletContextListener

(1).ServletContext 对象生命周期监听器

ServletContextListener 接口定义了 ServletContext 对象生命周期的监听行为

  • void contextInitialized(ServletContextEvent sce)

Servlet 对象创建后会触发该监听方法,并将 ServletContext 对象传递到该方法中。

  • void contextDestroyed(ServletContextEvent sce)

ServletContext 对象销毁之前会触发该监听方法,并将 ServletContext 对象传递到该方法中。

(2).ServletContext 对象的属性操作监听器

ServletContextListener 接口定义了 ServletContext 对象生命周期的监听行为

  • void attributeAdded(ServletContextAttributeEvent scae)

向 ServletContext 对象中添加属性时会触发该监听方法,并将 ServletContext 对象传递到 该方法中。
触发事件的方法 servletContext.setAttribute(“key”,“value”)

  • void attributeRemoved(ServletContextAttributeEvent scae)

当从 ServletContext 对象中删除属性时会触发该监听方法,并将 ServletContext 对象传递 到该方法中。
触发事件方法 servletContext.removeAttribute(“key”)

  • void attributeReplaced(ServletContextAttributeEvent scae)

当从 ServletContext 对象中属性的值发生替换时会触发该监听方法,并将 ServletContext 对象传递到该方法中。
触发事件的方法 servletContext.setAttribute(“key”,“value”)

2.HttpSessionListener

(1).HttpSession 对象生命周期监听器

HttpSessionListener 接口定义了 HttpSession 对象生命周期的监听行为。

  • void sessionCreated(HttpSessionEvent se)

HttpSession 对象创建后会触发该监听方法,并将已创建 HttpSession 对象传递到该方法57中

  • void sessionDestroyed(HttpSessionEvent se)

HttpSession 对象在销毁之前会触发该监听方法,并将要销毁的HttpSession 对象传递到该方法中。

(2).HttpSession 对象的属性操作监听器

HttpSessionAttributeListener 接口定义了对于 HttpSession 对象属性操作的监听行为。

  • void attributeAdded(HttpSessionBindingEvent se)

向 HttpSession 对象中添加属性时会触发该监听方法,并将 HttpSession 对象传递到该方 法中。触发事件的方法HttpSession.setAttribute(“key”,“value”)。
触发事件的方法 servletContext.setAttribute(“key”,“value”)

  • void attributeRemoved(HttpSessionBindingEvent se)

当从 HttpSession 对象中删除属性时会触发该监听方法,并将 HttpSession 对象传递到该方法中。触发事件方法 HttpSession.removeAttribute(“key”)

  • void attributeReplaced(HttpSessionBindingEvent se)

当从 HttpSession 对象中属性的值发生替换时会触发该监听方法,并将 HttpSession 对象 传递到该方法中。触发事件的方法HttpSession.setAttribute(“key”,“value”)

3.HttpRequestListener

(1).HttpServletRequest 对象的生命周期监听器

ServletRequestListener 接口定义了 ServletReqest(是 HttpServletRequest 接口的父接口类型)对象生命周期的监听行为。

  • void requestInitialized(ServletRequestEvent sre)

HttpSession 对象创建后会触发该监听方法,并将已创建 HttpSession 对象传递到该方法57中

  • void requestDestroyed(ServletRequestEvent sre)

HttpServletRequest 对象在销毁之前会触发该监听方法,并将要销毁的 ServletRequest 对 象传递到该方法中。

(2).HttpServletRequest 对象的属性操作监听器

ServletRequestAttributeListener 接口定义了对于 HttpServletRequest 对象属性操作的监听行为

  • void attributeAdded(ServletRequestAttributeEvent srae)

向 HttpServletRequest 对象中添加属性时会触发该监听方法,并将 ServletRequest 对象传 递到该方法中。
触发事件的方法 HttpServletRequest.setAttribute(“key”,“value”)

  • void attributeRemoved(ServletRequestAttributeEvent srae)

当从 HttpServletRequest 对象中删除属性时会触发该监听方法,并将 ServletRequest 对象传递到该方法中。
触发事件方法 HttpServletRequest.removeAttribute(“key”)

  • void attributeReplaced(ServletRequestAttributeEvent srae)

当 从 HttpServletRequest 对 象 中 属 性 的 值 发 生 替 换 时 会 触 发 该 监 听 方 法 , 并将 ServletRequest 对象传递到该方法中。
触发事件的方法HttpServletRequest.setAttribute(“key”,“value”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

倾雨Jun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值