监听器 Listener

监听器是指专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监听的对象发生变化时,立即采取相应的行动。

Web 监听器

Servlet 规范
  • Servlet规范中定义的一种特殊类
  • 用于监听 ServletContext、HttpSession和ServletRequest等域对象的创建与销毁事件
  • 用于监听域对象的属性发生修改的事件
  • 可以再事件发生前、发生后做一些必要的处理

用途

  • 统计在线人数和在线用户
  • 系统启动时加载初始化信息
  • 统计网站访问量
  • 跟Spring结合

创建监听器步骤

  • 创建一个实现监听器接口的类
  • 配置web.xml 或者注解进行注册

创建类

选择接口,例如ServletContextListener。

在IDEA中创建listener默认接口: ServletContextListener,HttpSessionListener, HttpSessionAttributeListener。

【代码】

public class Listener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public Listener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        String initParam=sce.getServletContext().getInitParameter("initParam");
        System.out.println("contextInitialized:initParam="+initParam);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
        System.out.println("contextDestroyed");
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
      /* Session is created. */
        System.out.println("sessionCreated");
    }

    public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
        System.out.println("sessionDestroyed");
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
        System.out.println("attributeAdded");
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
        System.out.println("attributeRemoved");
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
        System.out.println("attributeReplaced");
    }
}

注册

xml配置

<listener>
    <listener-class>com.zdy.listener.Listener</listener-class>
</listener>

注解

多个监听器时,无法定义加载顺序(同过滤器)

@WebListener

被标注的类必须实现一下至少一个接口:

ServletContextListener
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener

属性

属性名 类型 是否可选 描述
value String 该监听器的描述信息

【实例】

@WebListener("This is Servlet 3.0+ Listener")

监听器启动顺序

监听器启动顺序

监听器的分类

按照监听对象划分

  • ServletContext:用于监听应用程序环境对象
  • HttpSession:用于监听用户会话对象
  • ServletRequest:用于监听请求消息对象

按照事件的时间划分

  • 监听域对象自身的创建和销毁的时间监听器
  • 监听域对象中的属性的增加和删除的时间监听器
  • 监听绑定到HttpSession域中的某个对象的状态的时间监听器

对象创建销毁

ServletContext--ServletContextListener

ServletContext

HttpSession--HttpSessionListener

HttpSession

ServletRequest--ServletRequestListener

ServletRequest

对象属性增删改

对象属性增删改

HttpSession中的对象状态

HttpSession中的对象状态
Session钝化机制

一脸懵逼的我回头需要再研究一下吧。po一下慕课网视频教程()

统计在线人数实例

【Listener代码】

@WebListener()
public class Listener implements HttpSessionListener {
    private int userNumber;
    public Listener() {
    }
    public void sessionCreated(HttpSessionEvent se) {
      userNumber++;
      se.getSession().getServletContext().setAttribute("userNumber",userNumber);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
      userNumber--;
      se.getSession().getServletContext().setAttribute("userNumber",userNumber);
    }
}

【JSP代码】

当前在线用户人数:${userNumber}

统计在线人户与IP地址实例代码,github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •  监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采...
    xdoyf阅读 4,835评论 0 5
  • 本文包括:1、Listener简介2、Servlet监听器3、监听三个域对象创建和销毁的事件监听器4、监听三个域对...
    廖少少阅读 6,119评论 6 28
  • 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取...
    joshul阅读 272评论 0 1
  • 监听器(listener) 监听器简介 :监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个...
    奋斗的老王阅读 2,542评论 0 53
  • 一、监听器的概览 监听器是指专门用于对其他对象身上发生的事件或状态的改变进行监听和相应处理的对象,当被监视的对象发...
    不知名的蛋挞阅读 1,398评论 0 3