zoukankan      html  css  js  c++  java
  • 监听器的两个应用

    一、获得当前在线人数

    package com.bjsxt.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * @program: JavaEE
     * @description
     * @author: wuhao
     * @create: 2019-12-13 09:41
     **/
    public class OnlineListener implements HttpSessionListener, HttpSessionAttributeListener {
        @Override
        public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
            if ("employee".equals(httpSessionBindingEvent.getName())){
                //获得全局对象
                ServletContext servletContext = httpSessionBindingEvent.getSession().getServletContext();
                //获得在线人数
                Integer online = (Integer) servletContext.getAttribute("online");
    
                if (online == null) {
                    online = 1;
                } else {
                    online++;
                }
                //设置全局属性
                servletContext.setAttribute("online",online );
            }
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
    
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
    
        }
    
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
            //注销session时 人数-1
            //获得全局对象
            ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
    
             Integer online = (Integer) servletContext.getAttribute("online");
    
             if (online==null){
                 online=0;
             }else {
                 online--;
             }
    
             servletContext.setAttribute("online",online );
    
    
    
        }
    }
    

      web.xml配置

    <listener>
    <listener-class>com.bjsxt.listener.OnlineListener</listener-class>
    </listener>

     二、监听用户的操作(日志文件)

    package com.bjsxt.listener;
    
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.http.HttpServletRequest;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    
    /**
     * @program: JavaEE
     * @description
     * 日志
     * @author: wuhao
     * @create: 2019-12-12 19:28
     **/
    public class UserListener implements ServletRequestListener {
        @Override
        public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
    
        }
    
        @Override
        //监听发出的请求
        public void requestInitialized(ServletRequestEvent servletRequestEvent) {
            ServletRequest servletRequest = servletRequestEvent.getServletRequest();
    
            HttpServletRequest request =(HttpServletRequest)servletRequest;
    
            //获得远程请求的ip
            String  ip = request.getRemoteAddr();
            //获得请求的时间
            String date = new Date().toLocaleString();
            //获得请求的动作
            String requestURI = request.getRequestURI();
            String queryString = request.getQueryString();
    
            //把这写到日志文件中去
            try {
                PrintWriter printWriter = new PrintWriter(new FileWriter("D:/sxtOA.txt", true));
    
                printWriter.println("用户,ip为:"+ip+"在"+date+"访问了程序的"+requestURI+"?"+queryString);
    
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
    
            }
    
    
        }
    }
    

      web.xml配置

    <listener>
            <listener-class>com.bjsxt.listener.UserListener</listener-class>
        </listener>
    

      

  • 相关阅读:
    后端——框架——持久层框架——Mybatis——补充——config配置文件中setting各项含义——待补充
    后端——框架——持久层框架——Mybatis——补充——pageHelper(分页)插件
    后端——框架——持久层框架——Mybatis——补充——commonMapper(通用Mapper)插件——待补充
    spring当中定时任务出现的问题
    mysql中死锁的出现以及解决情况
    spring boot启动原理
    spring boot方式实现aop
    使用spring boot的方式实现spring的拦截器(附加xml实现方式)
    mybatis使用注解的方式进行增删改查基本的操作
    spring静态属性的注入
  • 原文地址:https://www.cnblogs.com/ww103/p/12079171.html
Copyright © 2011-2022 走看看