JavaWeb 三大组件之监听器 Listener
Listener 监听器介绍
1. Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
2. Listener 是 JavaEE 的规范,就是接口
3. 监听器的作用是,监听某种变化(一般就是对象创建/销毁, 属性变化), 触发对应方法完成相应的任务
JavaWeb 的监听器
ServletContextListener 监听器
1. 作用:监听 ServletContext 创建或销毁(当我们 Web 应用启动时,就会创建 ServletContext),
即生命周期监听,应用场景
(1)加载初始化的配置文件;比如 spring 的配置文件
(2)任务调度(配合定时器 Timer/TimerTask)
相关方法
void contextInitialized(ServletContextEvent sce) 创建 Servletcontext 时触发
voidcontextDestroyed(ServletContextEvent sce) 销毁 Servletcontext
代码演示
wyxServletContextListener
1. /** 2. * 1. 当一个类实现了 ServletContextListener 3. * 2. 该类就是一个监听器 4. * 3. 该类可以监听的事件 由该类实现的监听接口决定 ,比如 实现ServletContextListener 5. * , 则该类就可以监听 ServletContext对象的创建和销毁, 以此类推 6. * 4. wyxServletContextListener 就是一个监听者 7. * 5. 当web应用启动时,就会产生 ServletContextEvent 事件, 会调用监听器的对应事件处理方法 8. * contextInitialized, 同时会传递 事件对象 9. * 6. 程序员可以通过 ServletContextEvent 事件对象,来获取需要的信息, 然后再进行业务处理 10. * 7. tomcat怎么知道这个监听器存在 ? 因为我们需要在web.xml中配置 11. */ 12. public class wyxServletContextListener implements ServletContextListener { 13. @Override 14. public void contextInitialized(ServletContextEvent servletContextEvent) { 15. ServletContext servletContext = servletContextEvent.getServletContext(); 16. System.out.println("WyxServletContextListener 监听到 " + 17. servletContext + " 被创建.."); 18. //如果我们获取到ServletContext 对象..进行业务处理 19. 20. } 21. 22. @Override 23. public void contextDestroyed(ServletContextEvent servletContextEvent) { 24. ServletContext servletContext = servletContextEvent.getServletContext(); 25. System.out.println("WyxServletContextListener 监听到 " + 26. servletContext + " 被销毁.."); 27. 28. //比如可以对servletContext 数据进行处理, 或者日志的管理... 29. System.out.println("进行处理工作....."); 30. } 31. }
xml配置
1. <listener> 2. <listener-class>com.wyx.listener.WyxServletContextListener</listener-class> 3. </listener>
ServletContextAttributeListener 监听器
1. 作用:监听 ServletContext 属性变化
相关方法
void attributeAdded(ServletContextAttributeEvent event) 添加属性时调用
void attributeReplaced(ServletContextAttributeEvent event) 替换属性时调用
void attributeRemoved(ServletContextAttributeEvent event) 移除属性时调用
代码示例
WyxServletContextAttributeListener.java
1. public class WyxServletContextAttributeListener implements ServletContextAttributeListener { 2. @Override 3. public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) { 4. 5. System.out.println("WyxServletContextAttributeListener 监听到添加属性.." 6. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 7. } 8. 9. @Override 10. public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) { 11. 12. System.out.println("WyxServletContextAttributeListener 监听到删除属性.." 13. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 14. } 15. 16. @Override 17. public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) { 18. 19. System.out.println("WyxServletContextAttributeListener 监听到修改属性.." 20. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 21. 22. } 23. }
TestServlet.java
1. import javax.servlet.ServletException; 2. import javax.servlet.http.HttpServlet; 3. import javax.servlet.http.HttpServletRequest; 4. import javax.servlet.http.HttpServletResponse; 5. import java.io.IOException; 6. 7. public class TestServlet extends HttpServlet { 8. protected void doPost(HttpServletRequest request, HttpServletResponse response) 9. throws ServletException, IOException { 10. doGet(request, response); 11. } 12. protected void doGet(HttpServletRequest request, HttpServletResponse response) 13. throws ServletException, IOException { 14. getServletContext().setAttribute("k1","k1_value"); 15. } 16. }
xml配置
1. <listener> 2. <listener-class>com.wyx.listener.WyxHttpSessionAttributeListener</listener-class> 3. </listener>
HttpSessionListener 监听器
1. 作用:监听 Session 创建或销毁,即生命周期监听
2. 相关方法
void sessionCreated(HttpSessionEvent se)创建 session 时调用
void sessionDestroyed(HttpSessionEvent se)销毁 session 时调用
3. 使用方法和前面一样, 可以用于监控用户上线,离线
代码示例
1. public class WyxHttpSessionListener implements HttpSessionListener { 2. @Override 3. public void sessionCreated(HttpSessionEvent httpSessionEvent) { 4. HttpSession session = httpSessionEvent.getSession(); 5. //当session创建时,我们给它设置一个生命周期 30s 6. 7. session.setMaxInactiveInterval(30); 8. System.out.println("WyxHttpSessionListener 监听到 session创建= " + 9. session.getId()); 10. 11. System.out.println("用户id=" + session.getId() + " 上线"); 12. } 13. 14. @Override 15. public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 16. 17. HttpSession session = httpSessionEvent.getSession(); 18. 19. System.out.println("WyxHttpSessionListener 监听到 session销毁= " + 20. session.getId()); 21. System.out.println("用户id=" + session.getId() + " 离线"); 22. } 23. }
xml配置与前面一样
HttpSessionAttributeListener 监听器
1. 作用:监听 Session 属性的变化
2. 相关方法
void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时
void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时
void attributeRemoved(ServletRequestAttributeEvent srae) 移除属性
代码示例
1. public class WyxHttpSessionAttributeListener implements HttpSessionAttributeListener { 2. @Override 3. public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) { 4. //HttpSession session = httpSessionBindingEvent.getSession(); 5. 6. System.out.println("WyxHttpSessionAttributeListener 监听到session添加属性" + 7. httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue()); 8. } 9. 10. @Override 11. public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) { 12. HttpSession session = httpSessionBindingEvent.getSession(); 13. 14. System.out.println("WyxHttpSessionAttributeListener 监听到session删除属性" + 15. httpSessionBindingEvent.getName()); 16. } 17. 18. @Override 19. public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) { 20. 21. System.out.println("WyxHttpSessionAttributeListener 监听到session修改属性" + 22. httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue()); 23. } 24. }
xml配置与前面一样
ServletRequestListener 监听器
1. ServletRequestListener 监听器
2. 作用:监听 Request 创建或销毁,即 Request 生命周期监听
相关方法
void requestInitialized(ServletRequestEvent sre) 创建 request 时
void requestDestroyed(ServletRequestEvent sre) 销毁 request 时
3. 可以用来监控, 某个 IP 访问我们网站的频率, 日志记录 ,访问资源的情况
代码示例
1. public class WyxRequestListener implements ServletRequestListener { 2. 3. @Override 4. public void requestInitialized(ServletRequestEvent servletRequestEvent) { 5. System.out.println("WyxRequestListener 监听到 request对象创建"); 6. 7. ServletRequest servletRequest = servletRequestEvent.getServletRequest(); 8. System.out.println("记录访问日志...."); 9. System.out.println("访问IP= " + servletRequest.getRemoteAddr()); 10. System.out.println("访问的资源= " + ((HttpServletRequest)servletRequest).getRequestURL()); 11. } 12. @Override 13. public void requestDestroyed(ServletRequestEvent servletRequestEvent) { 14. 15. System.out.println(" WyxRequestListener 监听到 request对象被销毁"); 16. } 17. 18. 19. }
ServletRequestAttributeListener 监听器
1. 作用:监听 Request 属性变化
2. 相关方法
void attributeAdded(ServletRequestAttributeEvent srae) 添加属性时
void attributeReplaced(ServletRequestAttributeEvent srae) 替换属性时
void attributeRemoved(ServletRequestAttributeEvent srae)移除属性时
代码示例
1. public class WyxServletContextAttributeListener implements ServletContextAttributeListener { 2. @Override 3. public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) { 4. 5. System.out.println("WyxServletContextAttributeL istener 监听到添加属性.." 6. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 7. } 8. 9. @Override 10. public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) { 11. 12. System.out.println("WyxServletContextAttributeListener 监听到删除属性.." 13. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 14. } 15. 16. @Override 17. public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) { 18. 19. System.out.println("WyxServletContextAttributeListener 监听到修改属性.." 20. + servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() ); 21. 22. } 23. }
HttpSessionBindingListener 感知监听器
HttpSessionActivationListener 感知监听器
不常用了解即可