前言

感谢

Listener介绍

Listener,监听器,作为监听web的事件监听

Listener在JavaWeb中,是使用观察者设计模式进行实现的。在这个模式下有两个角色:观察者和被观察者

观察者在被观察者中注册自己感兴趣的事件,当这个事件发生时,会调用接口来通知观察者。

事件监听机制

1、事件:要监听的事情

2、事件源:事件发生的地方

3、监听器:一个对象,用于监听事件

4、注册监听:将事件,事件源,监听器绑定在一起。当事件源发生事件之后,执行监听器代码

快速起步

1、定义一个类,实现ServletContextListener接口

2、复写方法

  1. package com.howling.listener;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5. public class HelloListener implements ServletContextListener {
  6. /**
  7. * ServletContext对象服务器启动后自动创建,这个是监听ServletContext而创建的,在服务器启动之后会自动调用
  8. *
  9. * @param servletContextEvent
  10. */
  11. @Override
  12. public void contextInitialized(ServletContextEvent servletContextEvent) {
  13. System.out.println("servletContextListener.contextInitialized...");
  14. }
  15. /**
  16. * 服务器关闭之后,ServletContext对象被销毁,只有当服务器正常关闭之后,该方法才会调用
  17. *
  18. * @param servletContextEvent
  19. */
  20. @Override
  21. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  22. System.out.println("servletContextListener.contextDestroyed...");
  23. }
  24. }

3、在web.xml中进行注册

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0"
  6. metadata-complete="false">
  7. <listener>
  8. <listener-class>com.howling.listener.HelloListener</listener-class>
  9. </listener>
  10. </web-app>

4、启动服务器,查看结果

监听器 - 图1

Listener详解

以下内容来自:https://www.cnblogs.com/nantang/p/5899546.html

Servlet3.0为我们提供了八个监听器接口,根据作用域分为三类

1、Servlet上下文监听接口

  • ServletContextListener
  • ServletContextAttributeListener

2、HTTP Session监听接口

  • HttpSessionListener
  • HttpSessionActivationListener
  • HttpSessionAttributeListener
  • HttpSessionBindingListener

3、Servlet Request监听接口

  • ServletRequestListener
  • ServletRequestAttributeListener

监听器 - 图2


Servlet上下文监听接口

ServletContextListener

Servlet上下文监听器接口,用于监听上下文初始化和上下文即将关闭事件。

里面有两个方法

  • contextInitialized:上下文初始化
  • contextDestroyed:上下文即将关闭

当对应的条件满足时,会自动执行这两个方法 上下文初始化和即将关闭,也就是在服务器开启和即将关闭的时刻,上下文会自动创建和销毁

ServletContextAttributeListener

表示Servlet上下文属性相关事件,当属性发生改变时及聚会触发,分为三个方法

  • attributeAdded:增加
  • attributeRemoved:删除
  • attributeReplaced:修改

替换方法注意 这里假如属性的值被替换的时候回调,那么ServletContextAttributeEvent.getValue()是之前的值


HTTP Session监听接口

HttpSessionListener

Session被创建和销毁的时候,回调方法

  • sessionCreated
  • sessionDestoryed

HttpSessionActivationListener

session即将钝化或者被激活时,回调方法

  • sessionWillPassivate
  • sessionDidActivate

Session的钝化和激活我们曾经在 Cookie&Session 的那篇文章讲过

HttpSessionAttributeListener

当Session发生增删改的时候回调方法

  • attributeAdded
  • attributeRemoved
  • attributeReplaced

替换方法注意 这里假如属性的值被替换的时候回调,那么ServletContextAttributeEvent.getValue()是之前的值

HttpSessionBindingListener

当Session属性发生增删时,也就是属性值绑定和解绑的时候,回调方法

  • valueBound
  • valueUnbound

当调用session的invalidate方法或者session失效时,也会回调valueUnbound方法。


Servlet Request监听接口

ServletRequestListener

当请求初始化或者销毁时,即客户端请求进入web应用(进入servlet或者第一个filter),回调方法

  • requestInitialized
  • requestDestoryed

ServletRequestAttributeListener

当请求的属性发生增删改的时候,servlet容器构造ServletRequestAttribute事件对象,并回调

  • attributeAdded
  • attributeRemoved
  • attributeReplaced