Servlet监听器
Servlet监听器
Listener
观察者模式。
本博客中关于观察者模式的博文:
http://www.cnblogs.com/mengdd/archive/2013/02/08/2909206.html
其参考资料中列出了更多的博文。
Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。
通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。
常用监听接口
ServletContextListener
监听ServletContext。
当创建ServletContext时激发contextInitialized(ServletContextEvent sce);
所有的ServletContextListeners会在所有filters和servlets初始化之前收到初始化通知。
当销毁ServletContext时激发contextDestroyed(ServletContextEvent sce)。
所有filters和servlets销毁之后,ServletContextListeners才得到context销毁通知。
也即,ServletContextListeners是早出晚归型的。
ServletContextAttributeListener
监听对ServletContext属性的操作,比如增加、删除、修改属性。
方法分别为:
attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)
HttpSessionListener
监听HttpSession的操作。
当创建一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,(这样就可以给在线人数加1)。
当销毁一个Session时,激发sessionDestroyed(HttpSessionEvent se)方法。
HttpSessionAttributeListener
监听HttpSession的属性变化,监听增加、移除和修改事件:
attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)
使用实例
实例1:ServletContextListener:

package com.mengdd.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed: " + sce.getServletContext()); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized: " + sce.getServletContext()); } }
配置在web.xml中:
<listener> <listener-class>com.mengdd.listener.MyServletContextListener</listener-class> </listener>
实例2:ServletContextAttributeListener

package com.mengdd.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; public class MyServletContextAttributeListener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent event) { System.out.println("attributeAdded"); System.out.println(event.getName() + " : " + event.getValue()); } @Override public void attributeRemoved(ServletContextAttributeEvent event) { System.out.println("attributeRemoved"); System.out.println(event.getName() + " : " + event.getValue()); } @Override public void attributeReplaced(ServletContextAttributeEvent event) { System.out.println("attributeReplaced"); System.out.println(event.getName() + " : " + event.getValue()); // 注意Replaced方法参数的getValue()获取的是被替换掉的值,即上一个value } }
配置:
<listener> <listener-class>com.mengdd.listener.MyServletContextAttributeListener</listener-class> </listener>
测试用JSP页面:
页面1的body:
<body> <% application.setAttribute("attr1", "hello"); %> <% application.setAttribute("attr1", "world"); %> <% application.setAttribute("attr1", "aa"); %> <% application.setAttribute("attr1", "bb"); %> </body>
页面2的body:
<body> <% application.removeAttribute("attr1"); %> </body>
HttpSessionListener、HttpSessionAttributeListener等的实例就不一一列举了。
参考资料
北京圣思园张龙老师Java Web视频教程。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了