• 运行环境:

  JDK8 + IntelliJ IDEA 2018.3 


 

Servlet监听器

在Servlet规范中定义了多种类型的监听器,它们用于监听器的事件源分别是ServletContext,HttpSession和ServletRequest这三个域对象.

和其他事件监听器略有不同的是,Servlet监听器的注册不是直接注册在事件源上,而是由Web容器负责注册,开发人员只需要在web.xml文件中使用<listener>标签配置好监听器接口.

 

监听对象的创建和销毁

HttpSessionListener

ServletContextListener

ServletRequestListener

这三个监听器分别监控着Session,Context,Request对象的创建和销毁

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class UserListener implements ServletContextListener, HttpSessionListener, ServletRequestListener {
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContexst实例销毁");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("ServletRequestEvent destory");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("ServletRequestEvent 初始化");
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("HttpSessionEvent se被创建");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("HttpSessionEvent se被销毁");
    }
}

 

监听对象属性变化

  1. ServletContextAttributeListener

  2. HttpSessionAttributeListener

  3. ServletRequestAttributeListener

    分别监听着Context,Session,Request对象属性的变化

这三个接口中都定义了三个方法来处理被监听对象中属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同.三个方法是:attributeAdded(),attributeRemoved(),attributeReplaced().

编写程序测试监听属性的变化的以上三个接口的使用方法;

实现ServletContextAttributeListener,HttpSessionAttributeListener,ServletRequestAttributeListener类的创建

import javax.servlet.*;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class PropertyChangeListener
        implements ServletContextAttributeListener,
        HttpSessionAttributeListener,
        ServletRequestAttributeListener {
  
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("ServletContext对象添加了属性");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("ServletContext对象移除了属性");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("ServletContext对象的属性发生了变化");
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequest对象添加了属性");
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequst对象 移除了属性");
    }
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequest属性发生了变化");
    }
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("HttpSession添加了属性");
    }         and the name and value of the attribute that was removed
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("HttpSession对象移除了属性");
    }
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("HttpSession对象替换了属性");
    }
}