Serverlet监听器

Servlet监听器用处

更加方便的控制application、session 和request对 象的发生的特定事件

可以集中处理特定的事件

 

HttpSession监听器接口

HttpSessionListener
HttpSessionAttributeListener
HttpSessionBindingListener
HttpSessionActivationListener

除了BindingListener以外,其它三个都要在web.xml中声明

 

HttpSessionListener 监听session的销毁和创建,方法有
sessionCreated()
sessionDestroyed()

 

HttpSessionAttributeListener 监听Session属性的改变

attributeAdded()

attributeReplaced()
attributeRemoved()

产生事件的代码:

session.setAttribute("username","SunWukong");
session.setAttribute("username","ZhangFei");
session.removeAttribute("username");

 

HttpSessionBindingListener
valueBound()

valueUnbound()

调用机制:

如果一个类实现了HttpSessionBindingListener接口,则当这个类的对象通过session.setAttribute()被
绑定到Session对象中时,则该对象的valueBound()方法被自动调用,当这个对象从Session中删除时(调用session.invalidate()和session.removeAttribute()方法,或Session对象过期)valueUnbound()方法将被自动调用。

 

HttpSessionActivationListener

sessionDidActivate()
sessionWillPassivate()

调用机制:

    活化(Activate)与(钝化)Passivate是Web容器为了更好的利用系统资源或者进行服务器负载平衡等原因而对特定对象采取的措施。
   会话对象的钝化指的是暂时将会话对象通过对象序列化的方式储存到硬盘上,而会话对象活化与钝化相反,Web容器把硬盘上储存的会话对象文件重新加载到Web容器中。
    sessionDidActivate()与sessionWillPassivate()方法分别于会话对象活化后和会话对象钝化前由容器进行自动调用。

 

ServletContext监听器接口

ServletContextListener
contextInitialized()
contextDestroyed()

ServletContextAttributeListener
attributeAdded()
attributeReplaced()
attributeRemoved()

 

 

HttpServletRequest监听器

ServletRequestListener
requestInitialized()
requestDestroyed()

ServletRequestAttributeListener
attributeAdded()
attributeReplaced()
attributeRemoved()

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值