ServletContextAttributeListener
监听对ServletContext属性的操作,比如增加/删除/修改
ServletContextListener监听ServletContext,当创建ServletContext时,激发 contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(SessionEvent se)方法;当销毁(或超时)一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法
HttpSessionBindingListener
valueBound---被设置到session中(setAttribute)
valueUnbound---从session中解除(removeAttribute)HttpSessionBindingListener 和HttpSessionListener 之间的最大区别: HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。 HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。 从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的。 HttpSessionBindingListener 需要存储在session 里 ,比如 session.setAttribute("ListenerName", new ImplBindingListener(username)); 正是这种区别成就了HttpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样就不需 要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。 这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。 valueUnbound的触发条件是以下三种情况: 执行session.invalidate()时。 session超时,自动销毁时。 执行session.setAttribute("ListenerName", "其他对象");或session.removeAttribute("ListenerName");将listener从session中删除时。 因此,只要不将listener从session中删除,就可以监听到session的销毁HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法
HttpSessionActivationListener使代码可以支持分布式环境
为了负载均衡或者fail-over,web容器可以迁移一个session到其他的jvm. session的passivation是指非活动的session被写入持久设备(比如硬盘)。 activate自然就是相反的过程。在分布式环境中切换的属性必须实现serializable接口 一般情况下他和HttpSessionBindingListener一起使用public class Fun implements HttpSessionBindingListener,HttpSessionActivationListener{ //HttpSessionActivationListener public void sessionDidActivate(HttpSessionEvent event){ logout("sessionDidActivate(" +event.getSession().getId()+ ")" ); //激活 } public void sessionWillPassivate(HttpSessionEvent event){ //被传送到别的jvm或 写到硬盘 logout("sessionWillPassivate(" +event.getSession().getId()+ ")" ); } //HttpSessionBindingListener public void valueBound(HttpSessionBindingEvent event){ //被设置到session中(setAttribute) logout("valueBound(" +event.getSession().getId()+event.getValue()+ ")" ); } public void valueUnbound(HttpSessionBindingEvent event){ //从session中解除(removeAttribute) logout("valueUnbound(" +event.getSession().getId()+event.getValue()+ ")" ); } }
关于session超时配置
1
ServletRequestListener
requestDestroyed request 响应后// 当发出请求,服务器响应后执行此方法 requestInitialized request 响应前//当发送请求,服务器响应前执行此方法 当请求的页面中包含了链接的css文件或js脚本文件等,都会相应增加触RequestListener方法的次数。 比如你在请求的页面中使用元素引入了一个css文件,则请求该页面时会触发两次requestInitialized方法,也就是说浏览器会发送两次请求。 而HttpSessionListener不会发生这种情况。 会引起这种情况的元素有: css,js,jsp导入等。如果同时配置了ServletContextListener,HttpSessionListener,ServletRequestListener,容器启动时 会先调用ServletContextListener的contextInitialized方法。 然后当客户端有请求到来,会先调用ServletRequestListener的requestInitialized方法,然后再调用HttpSessionListener的sessionCreated方法, 如果发生上面所说的页面链接了其它文件的情况,则会再次触发ServletRequestListener的requestInitialized方法ServletRequestAttributeListener 此外 我还找到了个 ResponseListener
Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
接口: 目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与 HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所 示:Listener接口 Event类 ServletContextListener ServletContextEvent ServletContextAttributeListener ServletContextAttributeEvent HttpSessionListener HttpSessionEvent HttpSessionActivationListener HttpSessionEventHttpSessionAttributeListener HttpSessionBindingEventHttpSessionBindingListener HttpSessionBindingEventServletRequestListener ServletRequestEventServletRequestAttributeListener ServletRequestAttributeEvent
分别介绍:
一 ServletContext相关监听接口
补充知识: 通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。 在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。 注意: 全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,例如:Name browser
当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:
String name = (String)application.getInitParameter("Name");
或者使用EL时:
${initPara.name}
若是在Servlet中,取得Name的值方法:
String name = (String)ServletContext.getInitParameter("Name");
1.ServletContextListener: 用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。 ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。 ServletContextListener接口的方法: void contextInitialized(ServletContextEvent sce) 通知正在接受的对象,应用程序已经被加载及初始化。 void contextDestroyed(ServletContextEvent sce) 通知正在接受的对象,应用程序已经被载出。 ServletContextEvent中的方法: ServletContext getServletContext() 取得ServletContext对象2.ServletContextAttributeListener :用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。 ServletContextAttributeListener接口方法: void attributeAdded(ServletContextAttributeEvent scab) 若有对象加入Application的范围,通知正在收听的对象 void attributeRemoved(ServletContextAttributeEvent scab) 若有对象从Application的范围移除,通知正在收听的对象 void attributeReplaced(ServletContextAttributeEvent scab) 若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象 ServletContextAttributeEvent中的方法: java.lang.String getName() 回传属性的名称 java.lang.Object getValue() 回传属性的值二、HttpSession相关监听接口 1.HttpSessionBindingListener接口 注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener 当我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的 setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或 Session Time out的时候)时,容器分别会自动调用下列两个方法: void valueBound(HttpSessionBindingEvent event) void valueUnbound(HttpSessionBindingEvent event) 思考:如何实现记录网站的客户登录日志, 统计在线人数?2.HttpSessionAttributeListener接口 HttpSessionAttributeListener监听HttpSession中的属性的操作。 当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。3.HttpSessionListener接口 HttpSessionListener 监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。4.HttpSessionActivationListener接口 主要用于同一个Session转移至不同的JVM的情形。三、ServletRequest监听接口 1.ServletRequestListener接口 和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
2.ServletRequestAttributeListener接口
和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest