zoukankan      html  css  js  c++  java
  • Java 监听器

    1: 监听器的定义

    2:监听器的用途

    3:实例

    4:监听器分类

    5:Servlet3.0下监听器的使用

    6:实际项目常用的案例

    1: 监听器的定义:

    监听器实际上是一个类,这个类实现了特定的接口,然后将这个类在 web.xml 文件中进行描述,这样服务器在启动的时候就可以实例化这个类,启动监听器。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。例如统计用户在线人数。

    web监听器是Servlet规范中定义的一种特殊类,用于监听ServletContext,HttpSession,ServletRequest等域对象的创建、销毁、以及属性的变化等,可以在事件发生前、发生后进行一些处理。

    2、监听器的用途

    • 1、统计在线人数和在线用户
    • 2、系统加载时进行信息的初始化工作
    • 3、统计网站的访问量
    • 4、跟Spring结合

    3、实例

    在web.xml文件中进行配置

    listenr-class中是自己定义的Listener的类路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class MyRequestListener implements ServletContextListener{
        public void contextInitialized(ServletContextEvent event){
            System.out.println("启动监听器");
        }
     
        public void contextDestroy(ServletContextEvent event){
            System.out.println("结束监听器");
        }
    }

    编译器会为我们自动生成contextInitialized和contextDestroy两个函数

    监听器>过滤器>serlvet,在 web.xml 文件中配置的时候注意顺序

    4 监听器的分类

    按监听的对象划分,可以分为监听

    • ServletContext对象
    • HttpSession对象
    • ServletRequest对象

    按监听的事件划分

    • 域对象自身的创建和销毁
    • 域对象中属性的创建和消除
    • 绑定到session中的某个对象的状态

    由于很多监听的方式一致,因此我们只考虑其中的HttpSession对象:

    在web.xml中配置session超时

    1
    2
    3
    <session-config>
        <session-timeout> 30 </session-timeout>
    </session-config>

    当超过30秒后,session会自动过期

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class MyRequestListener implements HttpSessionListener{
        public void sessionCreate(HttpSessionEvent event){
            System.out.println("event 创建:");
        }
     
        public void sessionDestroy(HttpSessionEvent event){
            System.out.println("event 销毁:");
        }
    }

    另外,我们可以实现HttpSessionAttributeListener来实现session对象属性的增加(added)、删除(removed)、替换(replaced)的功能

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class MyRequestListener implements HttpSessionAttributeListener{
        public void attributeAdded(HttpSessionBindEvent event){
             
        }
     
        public void attributeRemoved(HttpSessionBindEvent event){
             
        }
     
        public void attributeReplaced(HttpSessionBindEvent event){
             
        }
    }

    5、Servlte3.0下的使用

    在Servlet3.0下通过注解@WebListener("")就可以实现了,不需要在web.xml中进行配置

    6、实战

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    public class MyRequestListener implements HttpSessionListener{
        private int counts = 0;//用于统计在线人数
        public void sessionCreate(HttpSessionEvent event){
            counts++;
            event.getSession().getServletContext.setAttribute("name",counts); //用户人数+1
        }
     
        public void sessionDestroy(HttpSessionEvent event){
            counts--;
            event.getSession().getServletContext.setAttribute("name",counts);//用户人数-1
        }
    }
     
    @WebListener
    public class MyRequestListener2 implements ServletRequestListener{
        private ArrayList<User>list; //创建一个在线用户的列表
        public void requestCreate(ServletRequestEvent event){
            HttpServletRequest request = (HttpServletRequest) request.getServletContext();
            String sessionId = request.getSession().getSessionId();
            if(sessionId ...){
                ....
                list.add(...);
            }
        }
     
        public void requestDestroy(ServletRequestEvent event){
     
        }
    }
     
    public class User{
        private int sessionId;
         
    }

    注意,以上代码只是大致的代码,并不全面和正确。

  • 相关阅读:
    继承和多态的纠错
    面向对象的七个设计原则
    C#中简单的继承和多态
    体验套餐管理系统
    项目经理评分(评价)
    考勤信息(员工打卡)
    Python中的进制转换
    Python常用模块
    Tornado中异步框架的使用
    Tornado框架的简单使用
  • 原文地址:https://www.cnblogs.com/lxl57610/p/5860051.html
Copyright © 2011-2022 走看看