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

Servlet(三) 监听器

时间:2018-11-12 22:16:02      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:let   删除   ini   响应   aced   xtend   code   类型   两种方法   

Servlet监听器

所有事件都继承自 java.util.Event

分成三类:application范围、session范围和request范围

  • 监听器接口和注册

    • javax.servlet.ServletContextListener: 响应 ServletContext生命周期事件,Context创建之后,关闭之前会被调用
    • javax.servlet.ServletContextAttributeListener: 响应Context范围的属性 增删替换
    • javax.servlet.http.HttpSessionListener: 响应session创建 超时 失效事件
    • javax.servlet.http.HttpSessionAttributeListener:
    • javax.servlet.http.HttpSessionActivationListener: 响应session失效 激活事件
    • javax.servlet.http.HttpSessionBindingListener: 响应HttpSession中有 添加 删除 值时
    • javax.servlet.ServletRequestListener: 响应ServletRequest的创建or删除
    • javax.servlet.ServletRequestAttributeListener:
    • javax.servlet.AsyncListener: 异步操作监听器
  • Servlet3.0 - 3.1 两种方法实现

    • @WebListener
      public class ListenerClass implements ListenerInterface{}
    • <listener>
          <listener-class>fully-qualified listener class </istener-class>
      </listener>
  • javax.servlet.ServletContextListener

    • public interface ServletContextListener extends EventListener {
          void contextInitialized(ServletContextEvent var1);
      
          void contextDestroyed(ServletContextEvent var1);
      }
      
      //监听器方法入参类型
      public class ServletContextEvent extends EventObject {
          private static final long serialVersionUID = 1L;
      
          public ServletContextEvent(ServletContext source) {
              super(source);
          }
      
          public ServletContext ServletContextEvent() {
              return (ServletContext)super.getSource();
          }
      }
    • 通过入参ServletContextEvent中的ServletContextEvent()可以获取应用上下文获取数据

    • 对ServletContext创建和销毁做出反应

  • javax.servlet.ServletContextAttributeListener

    • public interface ServletContextAttributeListener extends EventListener {
          void attributeAdded(ServletContextAttributeEvent var1);
      
          void attributeRemoved(ServletContextAttributeEvent var1);
      
          void attributeReplaced(ServletContextAttributeEvent var1);
      }
      
      
      
      //监听器方法入参类型
      public class ServletContextAttributeEvent extends ServletContextEvent {
          private static final long serialVersionUID = 1L;
          private final String name;
          private final Object value;
      
          public ServletContextAttributeEvent(ServletContext source, String name, Object value) {
              super(source);
              this.name = name;
              this.value = value;
          }
      
          public String getName() {
              return this.name;
          }
      
          public Object getValue() {
              return this.value;
          }
      }
      
  • javax.servlet.http.HttpSessionListener

    • public interface HttpSessionListener extends EventListener {
          void sessionCreated(HttpSessionEvent var1);
      
          void sessionDestroyed(HttpSessionEvent var1);
      }
      
      
      //监听器方法入参类型
      public class HttpSessionEvent extends EventObject {
          private static final long serialVersionUID = 1L;
      
          public HttpSessionEvent(HttpSession source) {
              super(source);
          }
      
          public HttpSession getSession() {
              return (HttpSession)super.getSource();
          }
      }
  • javax.servlet.http.HttpSessionActivationListener

    • public interface HttpSessionAttributeListener extends EventListener {
          void attributeAdded(HttpSessionBindingEvent var1);
      
          void attributeRemoved(HttpSessionBindingEvent var1);
      
          void attributeReplaced(HttpSessionBindingEvent var1);
      }
      
      
      
      //监听器方法入参类型
      public class HttpSessionBindingEvent extends HttpSessionEvent {
          private static final long serialVersionUID = 1L;
          private final String name;
          private final Object value;
      
          public HttpSessionBindingEvent(HttpSession session, String name) {
              super(session);
              this.name = name;
              this.value = null;
          }
      
          public HttpSessionBindingEvent(HttpSession session, String name, Object value) {
              super(session);
              this.name = name;
              this.value = value;
          }
      
          public HttpSession getSession() {
              return super.getSession();
          }
      
          public String getName() {
              return this.name;
          }
      
          public Object getValue() {
              return this.value;
          }
      }
    • HttpSessionBindingEvent extends HttpSessionEvent 所以 在HttpSessionBindingEvent 也能够拿到HttpSession对象
  • javax.servlet.http.HttpSessionActivationListener
    在分布式环境下,会用多个容器来进行负载均衡,有可能需要将session保存起来,在容器之间传递,ex一个容器内存不足,会将很少用到的对象转存刀其他容器上,这时候就会通知HttpSessionActivationListener

    • public interface HttpSessionActivationListener extends EventListener {
          //钝化
          void sessionWillPassivate(HttpSessionEvent var1);
          //活化
          void sessionDidActivate(HttpSessionEvent var1); 
      }
  • javax.servlet.http.HttpSessionBindingListener

    • public interface HttpSessionBindingListener extends EventListener {
          void valueBound(HttpSessionBindingEvent var1);
      
          void valueUnbound(HttpSessionBindingEvent var1);
      }
  • javax.servlet.ServletRequestListener

    • public interface ServletRequestListener extends EventListener {
          void requestDestroyed(ServletRequestEvent var1);
      
          void requestInitialized(ServletRequestEvent var1);
      }
      
      
      
      //监听器方法入参类型
      public class ServletRequestEvent extends EventObject {
          private static final long serialVersionUID = 1L;
          private final transient ServletRequest request;
      
          public ServletRequestEvent(ServletContext sc, ServletRequest request) {
              super(sc);
              this.request = request;
          }
      
          public ServletRequest getServletRequest() {
              return this.request;
          }
      
          public ServletContext getServletContext() {
              return (ServletContext)super.getSource();
          }
      }
  • javax.servlet.ServletRequestAttributeListener

    • public interface ServletRequestAttributeListener extends EventListener {
          void attributeAdded(ServletRequestAttributeEvent var1);
      
          void attributeRemoved(ServletRequestAttributeEvent var1);
      
          void attributeReplaced(ServletRequestAttributeEvent var1);
      }

Servlet(三) 监听器

标签:let   删除   ini   响应   aced   xtend   code   类型   两种方法   

原文地址:https://www.cnblogs.com/houhou87/p/9949374.html

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