一、监听器配置:
1.基于注解配置:@WebListener;//监听器>过滤器>serlvet,配置的时候要注意先后顺序;
2.基于web.xml配置:
<listener> <listener-class>全类名</listener-class> </listener>
二、监听域对象/域对象属性:继承对应的接口;
1.ServletContext(application):
(1)监听生命周期:ServletContextListener;
1)创建:void contextInitialized(ServletContextEvent sce);
2)销毁:void contextDestroyed(ServletContextEvent sce);
(2)监听属性:ServletContextAttributeListener;
1)添加属性:void attributeAdded(ServletContextAttributeEvent event);
2)替换属性:void attributeReplaced(ServletContextAttributeEvent event);
3)移除属性:void attributeRemoved(ServletContextAttributeEvent event);
2.HttpSession:
(1)监听生命周期:HttpSessionListener;
1)创建:void sessionCreated(HttpSessionEvent se);
2)销毁:void sessionDestroyed(HttpSessionEvent se);
(2)监听属性:HttpSessionAttributeListener;
1)添加属性:void attributeAdded(HttpSessionBindingEvent event);
2)替换属性:void attributeReplaced(HttpSessionBindingEvent event);
3)移除属性:void attributeRemoved(HttpSessionBindingEvent event);
3.ServletRequest:
(1)监听生命周期:ServletRequestListener;
1)创建:void requestInitialized(ServletRequestEvent sre);
2)销毁:void requestDestroyed(ServletRequestEvent sre);
(2)监听属性:ServletRequestAttributeListener;
1)添加属性:void attributeAdded(ServletRequestAttributeEvent srae);
2)替换属性:void attributeReplaced(ServletRequestAttributeEvent srae);
3)移除属性:void attributeRemoved(ServletRequestAttributeEvent srae);
@WebListener public class ListenerTest implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("requestDestroyed......"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("requestInitialized......"); } }
@WebListener public class ListenerTest implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("attributeAdded......"); } @Override public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("attributeRemoved......"); } @Override public void attributeReplaced(ServletRequestAttributeEvent srae) { System.out.println("attributeReplaced......"); } }
原文地址:https://www.cnblogs.com/Tractors/p/11279763.html