package com.an.tomcatservlet; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequest; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * @author apy * @description * @date 2021/10/21 17:29 */ public class MyListener implements ServletRequestListener ,ServletRequestAttributeListener ,ServletContextListener ,ServletContextAttributeListener ,HttpSessionListener ,HttpSessionBindingListener ,HttpSessionActivationListener ,HttpSessionAttributeListener{ public void requestInitialized(ServletRequestEvent sre) { ServletRequest servletRequest = sre.getServletRequest(); // do something... } public void requestDestroyed(ServletRequestEvent sre) { } public void attributeAdded(ServletRequestAttributeEvent srae) { } public void attributeRemoved(ServletRequestAttributeEvent srae) { } public void attributeReplaced(ServletRequestAttributeEvent srae) { } public void contextInitialized(ServletContextEvent sce) { } public void contextDestroyed(ServletContextEvent sce) { } public void attributeAdded(ServletContextAttributeEvent event) { } public void attributeRemoved(ServletContextAttributeEvent event) { } public void attributeReplaced(ServletContextAttributeEvent event) { } public void sessionCreated(HttpSessionEvent se) { } public void sessionDestroyed(HttpSessionEvent se) { } public void valueBound(HttpSessionBindingEvent event) { } public void valueUnbound(HttpSessionBindingEvent event) { } /** * 【servlet Listener】 * why? * 解决 共享对象 内容发生变化 时, 在发生前、发生后 做处理; * * Servlet2.3 提供了 对 ServletContext、HttpSession对象的变化的 监听器; * Servlet2.4 增加了 对 ServletRequest对象的变化的 监听器; * * what? * ServletContext监听器:监听 ServletContext对象,可以 使 web应用 得知 web组件的 加载、卸载情况等; * ServletContextListener * ServletContextAttributeListener * * ServletContextEvent * ServletContextAttributeEvent * * HttpSession监听器:监听 HttpSession对象变化,可以 使 web应用 了解 会话期间的状态 并 做出响应; * HttpSessionListener * HttpSessionActivationListener 实现数据 内存->磁盘,磁盘->内存; * * HttpSessionEvent * * * HttpSessionAttributeListener * HttpSessionBindingListener * * HttpSessionBindingEvent * * * ServletRequest监听器:监听 ServletRequest对象变化,可以 使 web应用 控制web请求的生命周期; * ServletRequestListener * ServletRequestAttributeListener * * ServletRequestEvent * ServletRequestAttributeEvent * * How? * 创建普通Java类,实现 对应的Listener,重写相应的方法; * 在web.xml中配置相应的 Listener; */ }