码迷,mamicode.com
首页 > 其他好文 > 详细

06 监听器

时间:2020-04-03 11:50:18      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:iso   https   pre   imp   attribute   sys   作用   event   value   

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

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 监听器

标签:iso   https   pre   imp   attribute   sys   作用   event   value   

原文地址:https://www.cnblogs.com/alichengxuyuan/p/12625390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!