Listener监听器
一、背景描述
javaweb中的监听器,是用于监听javaweb对象
例如:HttpServletRequest、HttpSession、ServletContext
监听他们的创建和销毁、属性变化、session绑定javaBean
二、javaWeb常见的监听器
1、监听域对象的创建和销毁
(1)如果要监听ServletContext创建和销毁 ---- 需要自定义实现类,来实现监听接口 ServletContextListener
package javax.servlet; import java.util.EventListener; public interface ServletContextListener extends EventListener { default void contextInitialized(ServletContextEvent sce) { } default void contextDestroyed(ServletContextEvent sce) { } }
(2)如果要监听HttpSession创建和销毁 ---- 需要自定义实现类,来实现监听接口 HttpSessionListener
package javax.servlet.http; import java.util.EventListener; public interface HttpSessionListener extends EventListener { default void sessionCreated(HttpSessionEvent se) { } default void sessionDestroyed(HttpSessionEvent se) { } }
(3)如果要监听HttpServletRequest创建和销毁 ---- 需要自定义实现类,来实现监听接口 ServletRequestListener
package javax.servlet; import java.util.EventListener; public interface ServletRequestListener extends EventListener { default void requestDestroyed(ServletRequestEvent sre) { } default void requestInitialized(ServletRequestEvent sre) { } }
2、监听域对象的属性变化
(1)监听ServletContext属性的变化 – ServletContextAttributeListener
package javax.servlet; import java.util.EventListener; public interface ServletContextAttributeListener extends EventListener { default void attributeAdded(ServletContextAttributeEvent event) { } default void attributeRemoved(ServletContextAttributeEvent event) { } default void attributeReplaced(ServletContextAttributeEvent event) { } }
(2)监听HttpSession属性的变化 – HttpSessionAttributeListener
package javax.servlet.http; import java.util.EventListener; public interface HttpSessionAttributeListener extends EventListener { default void attributeAdded(HttpSessionBindingEvent event) { } default void attributeRemoved(HttpSessionBindingEvent event) { } default void attributeReplaced(HttpSessionBindingEvent event) { } }
(3)监听HttpServletRequest属性的变化 – ServletRequestAttributeListener
package javax.servlet; import java.util.EventListener; public interface ServletRequestAttributeListener extends EventListener { default void attributeAdded(ServletRequestAttributeEvent srae) { } default void attributeRemoved(ServletRequestAttributeEvent srae) { } default void attributeReplaced(ServletRequestAttributeEvent srae) { } }
3、监听session绑定javaBean
(1)监听javaBean对象是否绑定了session域 – HttpSessionBindingListener
package javax.servlet.http; import java.util.EventListener; public interface HttpSessionBindingListener extends EventListener { default void valueBound(HttpSessionBindingEvent event) { } default void valueUnbound(HttpSessionBindingEvent event) { } }
(2)监听javaBean对象的活化或钝化 --HttpSessionActivationListener
package javax.servlet.http; import java.util.EventListener; public interface HttpSessionActivationListener extends EventListener { default void sessionWillPassivate(HttpSessionEvent se) { } default void sessionDidActivate(HttpSessionEvent se) { } }