rvletContext 将要被销毁。 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 接口 HttpSessionAttribu
teListener 监听 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
常用的监听接口 1.ServletContextAttributeListener 监听对 ServletContext 属性的操作,比如增加/删除/修改 2.ServletContextListener 监 听 ServletContext, 当 创 建 ServletContext 时 , 激 发 contextInitialized(ServletContextEvent sce) 方 法 ; 当 销 毁 ServletContext 时,激发 contextDestroyed(ServletContextEvent sce) 方法。 3.HttpSessionListener 监 听 HttpSession 的 操 作 。 当 创 建 一 个 Session 时 , 激 发 session Created(SessionEvent se) 方 法 ; 当 销 毁 一 个 Session 时 , 激 发 sessionDestroyed (HttpSessionEvent se)方法。 4.HttpSessionAttributeListen