Servlet监听器概述

作为一个刚刚接触到jsp的新手,也想分享一些我学习的笔记 Servlet监听器概述: 在Web应用中响应特定对象的特定事件 使用Servlet监听器的好处:更加方便的控制Application、session、request对象的发生的特定事件,可以集中处理特定事件 HttpSession监听器接口: HttpSessionLIstener HttpSessionAttributeListener HttpSessionBindingListener HttpSessionActivationListener HttpSessionListener接口中有两个方法:sessionCreated()//当一个用户的session被创建以后,web容器将自动调用sessionCreated方法, sessionDestroyed()//当一个用户的session被销毁时web容器将自动调用该方法 在配置监听器时需要在web.xml中配置: 实现该监听器接口的类所在的位置 HttpSessionAttributeListener接口中有三个方法: attributeAdded()//方法对应与session.setAttribute("jfdk","fdf");即当一个session绑定了一个字段时将调用该方法。 attributeReplaced()//当一个session的一个属性值被替换时将自动调用该方法,如:session.setAttribute("jfdk","fdfs");时 ,web容器就会自动调用改方法 sessionRemoved()//当一个session对象的一个属性值被移除时将自动调用该方法: HttpSessionBindingListener接口 valueBound() valueUnbound() 调用机制: 如果一个类实现了HttpSessionBindingListener接口,则当这个类的对象通过session.setAttribute()被绑定到Session对象中时,则该对象的valueBound方法将被自动调用,当这个对象从Session中删除时,alueUnbound方法将自动被调用 HttpSessionActivationListener接口 sessionDidActivate() sessionWillPassivate() 调用机制: 活化(Activate)与钝化(Passivate)是web容器为了更好的利用系统资源或者进行服务器负载平衡等原因而对特定对象采取的措施。 回话对象的钝化指的是暂时将会话对象通过对象序列化的方式存到硬盘上,而会话对象活化与钝化相反,web容器把硬盘上存储的会话对象文件重新加载到web容器中。 sessionDidActivate()与sessionWillPassivate()方法分别与会话对象活化后和会话对象钝化前由容器进行自动调用的。 ServletContext监听器接口 ServletContextListener contextInitialized() contextDestroyed() 调用机制: 当在web应用中部署了实现该接口的实现类后,在web容器加载web应用时(例如启动服务器)就会自动调用contextInitialized()方法,而当web容器销毁Web应用时(例如关闭服务器),会自动调用contextDestroyed()方法。 ServletContextAttributeListener接口 attributeAdded()//与httpSessionAdded()方法类似 attributeReplaced() attributeRemoved() 触发事件的代码: context.setAttribute("counter",new Integer(0)); context.setAttribute("counter",new Integer(100)); context.removeAttribute("counter");//与httpSessionAttributeListener监听器类似 ServletRequestListener requestInitalized()//当在web应用中部署实现该接口的实现类后,在HttpServletRequest对象(JSP中request对象)建立时将自动调用该方法 requestDestroyed()//当HttpServletRequest对象(jsp中request对象)被销毁时将自动调用该方法。 ServletRequestAttributeListener接口实现对request对象的属性添加,替代,移除动作的监听,同HttpSessionAttributeListener接口类似 attributeAdded() attributeRemoved() attributeReplaced()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值