文章目录
一、监听器是什么
-
类似 JS 的事件
- 被观察的对象发生某些情况时,自动触发代码的执行
-
对域对象要发生的方法进行监听
-
并不监听web项目中的所有组件,仅仅是对三大域对象做相关的事件监听
-
监听器是GOF设计模式中,观察者模式的典型案例
- 观察者模式: 当被观察的对象发生某些改变时, 观察者自动采取对应的行动的一种设计模式
二、监听器的分类
web中定义八个监听器接口作为监听器的规范,
这八个接口按照不同的标准可以形成不同的分类 :
-
按监听的对象划分
- application域监听器
ServletContextListenerServletContextAttributeListener
- session域监听器
HttpSessionListenerHttpSessionAttributeListenerHttpSessionBindingListenerHttpSessionActivationListener
- request域监听器
ServletRequestListenerServletRequestAttributeListener
- application域监听器
-
按监听的事件分
- 域对象的创建和销毁监听器
- ServletContextListener
- HttpSessionListener
- ServletRequestListener
- 域对象数据增删改事件监听器
- ServletContextAttributeListener
- HttpSessionAttributeListener
- ServletRequestAttributeListener
- 其他监听器
- HttpSessionBindingListener
- HttpSessionActivationListener
- 域对象的创建和销毁监听器
三、监听器的六个主要接口
3.1 application域监听器
ServletContextListener监听ServletContext对象的创建与销毁
| 方法名 | 作用 |
|---|---|
| contextInitialized(ServletContextEvent sce) | ServletContext创建时调用 |
| contextDestroyed(ServletContextEvent sce) | ServletContext销毁时调用 |
- ServletContextEvent对象代表从ServletContext对象身上捕获到的事件,
- 通过这个事件对象我们可以获取到ServletContext对象。
ServletContextAttributeListener监听ServletContext中属性的添加、移除和修改
| 方法名 | 作用 |
|---|---|
| attributeAdded(ServletContextAttributeEvent scab) | 向ServletContext中添加属性时调用 |
| attributeRemoved(ServletContextAttributeEvent scab) | 从ServletContext中移除属性时调用 |
| attributeReplaced(ServletContextAttributeEvent scab) | 当ServletContext中的属性被修改时调用 |
ServletContextAttributeEvent对象代表属性变化事件,它包含的方法如下:
| 方法名 | 作用 |
|---|---|
| getName() | 获取修改或添加的属性名 |
| getValue() | 获取被修改或添加的属性值 |
| getServletContext() | 获取ServletContext对象 |
测试代码 :
3.1.1 定义监听器
@WebListener
public class ApplicationListener implements ServletContextListener , ServletContextAttributeListener {
// 监听初始化
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
System.out.println("application"+application.hashCode()+" initialized");
}
// 监听销毁
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
System.out.println("application"+application.hashCode()+" destroyed");
}
// 监听数据增加
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
String name = scae.getName();
Object value = scae.getValue();
ServletContext application = scae.getServletContext();
System.out.println("application"+application.hashCode()+" add:"+name+"="+value);
}
// 监听数据移除
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
String name = scae.getName();
Object value = scae.getValue();
ServletContext application = scae.getServletContext();
System.out.println("application"+application.hashCode()+" remove:"+name+"="+value);
}
// 监听数据修改
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
String name = scae.getName();
Object value = scae.getValue();
ServletContext application = scae.getServletContext();
Object newValue = application.getAttribute(name);
System.out.println("application"+application.hashCode()+" change:"+name+"="+value+" to "+newValue);
}
}
3.1.2 定义触发监听器的代码
// ServletA用于向application域中放入数据
@WebServlet(urlPatterns = "/servletA",name = "servletAName")
public class ServletA extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 向application域中放入数据
ServletContext application =

最低0.47元/天 解锁文章
1666

被折叠的 条评论
为什么被折叠?



