06 监听器

Posted alichengxuyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了06 监听器相关的知识,希望对你有一定的参考价值。

本节讲述常用监听器的用法。

1、环境约束

  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • servlet3.0

2、8大监听器的对比

  • ServletContextListener 是 ServletContext 的监听者,如果 ServletContext发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
      System.out.println("系统启动时执行");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
      System.out.println("系统关闭时执行");
    }
}
  • ServletContextAttributeListener:在application作用域中增删改属性会触发
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scab) {
        System.out.println("在applicaiton作用域中add值时候触发");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scab) {
        System.out.println("在applicaiton作用域中remove值时候触发");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scab) {
        System.out.println("在applicaiton作用域中replace值时候触发");
    }
}
  • HttpSessionBindingListener:只要对象加入Session范围或从Session范围中移出时被触发
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class MyHttpSessionBindingListener implements HttpSessionBindingListener {
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("session.setAttribute触发该事件");
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("session.removeAttribute触发该事件");
    }
}
  • HttpSessionAttributeListener:监听HttpSession中的属性的操作
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        System.out.println("在session作用域加入键值对触发");
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        System.out.println("在session作用域删除键值对触发");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        System.out.println("在session作用域替换键值对触发");
    }
}
  • HttpSessionListener:session被创建或者销毁触发
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyHttpSessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("session被创建的时候触发");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("session被销毁的时候触发");
    }
}
  • ServletRequestListener:和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyServletRequestListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("一次请求被销毁触发");
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("一次请求初始化触发");
    }
}
  • ServletRequestAttributeListener:在request作用域当中增删改触发
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;

public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("request加入键值对触发");
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("request删除键值对触发");
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("request替换键值对触发");
    }
}
  • HttpSessionActivationListener:主要用于同一个Session转移至不同的JVM的情形,感知自己何时随着HttpSession钝化/激活
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

public class MyHttpSessionActivationListener implements HttpSessionActivationListener {
    @Override
    public void sessionWillPassivate(HttpSessionEvent se) {
        System.out.println("sesison被激活是触发");
    }

    @Override
    public void sessionDidActivate(HttpSessionEvent se) {
        System.out.println("session钝化是触发");
    }
}

这就是监听器的基本用法。

以上是关于06 监听器的主要内容,如果未能解决你的问题,请参考以下文章

linux学习:Nginx--常见功能配置片段与优化-06

片段内 Listview 中动态视图的 onClick 侦听器

从父片段到选项卡片段的接口侦听器不起作用

listview OnItemClick 侦听器在片段中不起作用

从活动中获取片段内的按钮

如何使用事件侦听器来加载动画片段的循环