作为一个刚刚接触到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()
Servlet监听器概述
最新推荐文章于 2023-07-18 20:41:47 发布