• 监听器简介


    监听器: 监听事件,处理事件的对象. 使用观察者设计模式实现的.

    观察者设计模式: 分为三个角色, 分别是:事件源, 事件, 监听器.

    事件: 就是发生的事情, 其中包含事件源

    事件源: 发生事情的根源, 触发事件的资源(对象)(发生事件的场所)

    监听器: 处理事件的逻辑., 根据事件,获取事件源,并处理事件.

    Servlet中的监听器

    代码实现

    public class LogListener  implements ServletRequestListener {
        //监听请求销毁
        @Override
        public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        }
        //监听请求的发出
        @Override
        public void requestInitialized(ServletRequestEvent servletRequestEvent) {
             // XXX 在  XXX时间  访问  程序的XX位置
            ServletRequest servletRequest = servletRequestEvent.getServletRequest();
            HttpServletRequest  request=(HttpServletRequest)servletRequest;
            //获得远程访问的IP地址
            String ip = request.getRemoteAddr();
            //获得访问的时间
            String localeString = new Date().toLocaleString();
            //程序的位置    servlet/uu?method=add
    
            String requestURI = request.getRequestURI();
            String queryString = request.getQueryString();
            try {
                PrintWriter  out=new PrintWriter(new FileWriter("D:/log.txt",true));
                out.println("用户:"+ip+",在:"+localeString+",访问了程序:"+requestURI+"?"+queryString);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

      

     监听器实现在线人数的统计

    public class OnLineListener implements HttpSessionListener,HttpSessionAttributeListener {
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        }
        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            ServletContext application = httpSessionEvent.getSession().getServletContext();
    
            Integer online = (Integer) application.getAttribute("online");
            if(online==null){
                online=0;
            }else {
                online--;
            }
            application.setAttribute("online",online)
        }
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
    
      if("emp".equals(httpSessionBindingEvent.getName())){
                //全局对象
                ServletContext application = httpSessionBindingEvent.getSession().getServletContext();
                //获得当前的在线人数
                Integer online = (Integer) application.getAttribute("online");
                if(online==null){
                    //当前没人登陆
                    online=1;
                }else {
                    //当前有人登陆
                    online++;
                }
    
                //把记录的值重新的设置到application
                application.setAttribute("online",online);
            }
        }
        @Override
        public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        }
        @Override
        public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        }
    }
    

      

     其他监听器使用

    /**
    参考地址:
    https://blog.csdn.net/kiven_wolf/article/details/77648667
     * HttpSessionBindingListener:
     *     监听对象从session 上进行绑定和解绑的操作
     *  如果指定的对象绑定到session上的时候就会执行valueBound
     *  如果对象从session中解绑的时候或者session注销的时候就会执行valueUnbound
     *  需要注意的是使用这个监听器的时候不需要在web.xml 中进行任何的配置
     * */
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("valueBound");
    }
    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        // req.getSession().invalidate();
        // req.getSession().removeAttribute("emp");
        System.out.println("valueUnbound");
    }
    /**
     * 如果想要实现序列化和反序列化需要在Context.xml中
     * <Manager className="org.apache.catalina.session.PersistentManager" saveOnRestart="true">
     <Store className="org.apache.catalina.session.FileStore" directory="E:\apache-tomcat-7.0.79\webapps"/>
     </Manager>
       如果对象想要实现序列化过程,对象上必须实现 Serializable
     *
     * **/
    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
        System.out.println("sessionWillPassivate");
    }
    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {   System.out.println("sessionDidActivate");
    }
    

      

  • 相关阅读:
    Linux运维必会的MySql题之(四)
    Linux运维必会的MySql题之(三)
    Linux运维必会的MySql题之(二)
    Linux运维必会的MySql题之(一)
    Centos7 yum安装Mysql
    Devoos核心要点及kubernetes架构概述
    kubernetes基本概念
    BZOJ2631 tree 【LCT】
    BZOJ2431 [HAOI2009]逆序对数列 【dp】
    BZOJ1483 [HNOI2009]梦幻布丁 【链表 + 启发式合并】
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14292125.html
Copyright © 2020-2023  润新知