阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

监听器概述

104次阅读
没有评论

共计 1467 个字符,预计需要花费 4 分钟才能阅读完成。

1、监听器三要素

  • 事件源
  • 事件
  • 事件处理

在 JavaWeb 被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。

  • 监听域对象“创建”与“销毁”的监听器;
  • 监听域对象“操作域属性”的监听器;
  • 监听 HttpSession 的监听器。

2、ServletContext 的监听器

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

​ void contextInitialized(ServletContextEvent sce):创建 SErvletcontext 时

​ void contextDestroyed(ServletContextEvent sce):销毁 Servletcontext 时

属性监听 :ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

​ void attributeAdded(ServletContextAttributeEvent event):添加属性时;

​ void attributeReplaced(ServletContextAttributeEvent event):替换属性时;

​ void attributeRemoved(ServletContextAttributeEvent event):移除属性时;

3、HttpSession 的监听器

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

void sessionCreated(HttpSessionEvent se):创建 session 时

void sessionDestroyed(HttpSessionEvent se):销毁 session 时

属性监听 :HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

void attributeAdded(HttpSessionBindingEvent event):添加属性时;

void attributeReplaced(HttpSessionBindingEvent event):替换属性时

void attributeRemoved(HttpSessionBindingEvent event):移除属性时

ServletRequest 的监听器

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

void requestInitialized(ServletRequestEvent sre):创建 request 时

void requestDestroyed(ServletRequestEvent sre):销毁 request 时

属性监听 :ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。

void attributeAdded(ServletRequestAttributeEvent srae):添加属性时

void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时

void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计1467字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中