1.Web监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext、HttpSession、ServletRequest等域对象的创建与销毁事件
2.Web监听器的主要用途
1)统计在线人数和在线用户
2)系统启动时加载初始化信息
3)统计网站访问量
4)跟Spring结合
[*企业中应用]
3.Web监听器创建
1)实现Web接口类(ServletContextListener)
2)在web.xml中进行配置(listener)
4.监听器分类(按监听的事件划分)
1)监听域对象自身的创建和销毁的事件监听器
ServletContext——ServletContextListener
HttpSession——HttpSessionListener
ServletRequest——ServletRequestListener
contestDestroyed
ServletContext对象的创建和销毁?
创建:
销毁:
2)监听域对象中属性的增加和删除的事件监听器
ServletContext——ServletContextAttributeListener
HttpSession——HttpSessionAttributeListener
ServletRequest——ServletRequestAttributeListener
attributeAdded,attributeRemoved,attributeReplaced
3)监听绑定到HttpSession域中的某个对象的状态的事件监听器
HttpSession中对象的状态:绑定,解除绑定,钝化(持久化),活化
HttpSessionBindingListener valueBound(绑定),valueUnbound(解除绑定)
HttpSessionActivationListener sessionWillPassivate(钝化),sessinDidActivate(活化)
步骤:对象bean实现HttpSessionBindingListener ,HttpSessionActivationListener,Serializable接口并实现相应方法,将bean对象放到session attribute中。
5.Session的创建和销毁
创建:打开浏览器
销毁:
1)关闭服务器
2)关闭浏览器一段时间之后
3)不关闭浏览器,session超时
web.xml <session-config> <session-timeout> 以分钟为单位
6.Session钝化
正常情况下session处于服务器内存中,一些不常使用的session会被序列化,当需要使用时,再被反序列化到内存中
Session钝化机制由SessionManager管理
Tomcat两种session管理器——Persistentmanager,StandardManager