定义
监听器主要用来监一些特定对象的创建或销毁以及属性变化的,是一个实现特定接口的普通java类。
按照监听的对象划分,可以分为ServletContext对象监听器,HttpSession对象监听器,ServletRequest对象监听器;按照监听的事件划分,可以分为对象自身的创建和销毁的监听器,对象中属性的创建和消除的监听器,session中某个对象的状态变化的监听器。
ServletRequestListener、ServletContextListener、HttpSessionListener
<listener> <listener-class>ysdrzp.listener.MyServletRequestListener</listener-class> </listener> <listener> <listener-class>ysdrzp.listener.MyServletContextListener</listener-class> </listener> <listener> <listener-class>ysdrzp.listener.MyHttpSessionListener</listener-class> </listener>
/** * 监听ServletRequest对象的创建和销毁 */ public class MyServletRequestListener implements ServletRequestListener { @Override public void requestInitialized(ServletRequestEvent servletRequestEvent) { System.out.println("MyServletRequestListener.requestInitialized()"); } @Override public void requestDestroyed(ServletRequestEvent servletRequestEvent) { System.out.println("MyServletRequestListener.requestDestroyed()"); } }
/** * 监听ServletContext对象的创建和销毁 */ public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("MyServletContextListener.contextInitialized()"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("MyServletContextListener.contextDestroyed()"); } }
/** * 监听HttpSession对象的创建和销毁 */ public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("MyHttpSessionListener.sessionCreated()"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("MyHttpSessionListener.sessionDestroyed()"); } }
ServletRequestAttributeListener、ServletContextAttributeListener、HttpSessionAttributeListener
/** * ServletRequest域对象属性监听器 */ public class MyServletRequestAttributeListener implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) { System.out.println(servletRequestAttributeEvent.getName()+"属性增加"); System.out.println(servletRequestAttributeEvent.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) { System.out.println(servletRequestAttributeEvent.getName()+"属性移除"); System.out.println(servletRequestAttributeEvent.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) { System.out.println(servletRequestAttributeEvent.getName()+"属性改变"); System.out.println(servletRequestAttributeEvent.getValue()); } }
/** * ServletContext域对象属性改变监听器 */ public class MyServletContextAttributeLinstener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) { } @Override public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) { } @Override public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) { } }
/** * HttpSession域对象属性监听器 */ public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) { } @Override public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) { } @Override public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) { } }
HttpSessionBindingListener
public class User implements HttpSessionBindingListener { private String name; private String sex; public User(String name,String sex){ this.name=name; this.sex=sex; } @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("User对象已经放入session中"); } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("User对象从session中移除"); } }