zoukankan      html  css  js  c++  java
  • 使用javaWeb的二大(Listener、Filter)组件实现分IP统计访问次数

    分析:

    统计工作需要在所有资源之前都执行,那么就可以放到Filter中。

    我们这个过滤器不打算做拦截操作!因为我们只是用来做统计

    用什么东西来装载统计的数据。Map<String,Integer>

    整个网站只需要一个Map即可!

    Map什么时候创建(使用ServletContextListener,在服务器启动时完成创建,并只在到ServletContext中),Map保存到哪里!(Map保存到ServletContext中)

    >Map需要在Filter中用来保存数据

    >Map需要在页面使用,打印Map中的数据

    代码准备:

    一个Listener: public void contextInitialized(ServletContextEvent sce)  { }

    一个Filter:

      >public void init(FilterConfig fConfig) throws ServletException {}

      >public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {}

    一个show.jsp

    一个web.xml

    -------------

    代码实现:

    ---Listener类实现

    import java.util.LinkedHashMap;
    import java.util.Map;

    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;

    @WebListener
    public class AListener implements ServletContextListener {

      /**
      * 在服务器启动时创建Map,保存到ServletContext
      */
      public void contextInitialized(ServletContextEvent sce) {
        //创建Map
        Map<String,Integer> map=new LinkedHashMap<String,Integer>();
        //得到ServletContext
        ServletContext application=sce.getServletContext();
        //把map保存到application中
        application.setAttribute("map", map);
      }
      public void contextDestroyed(ServletContextEvent sce) { }
    }

    ---Filter类实现

    import java.io.IOException;
    import java.util.Map;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;

    /**
    * 从application中获取Map
    * 从request中得到当前客户端的IP
    * 进行统计工作,结果保存到map中
    */
    public class AFilter implements Filter {

      private FilterConfig config;

      public void init(FilterConfig fConfig) throws ServletException {
        this.config=fConfig;
      }
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        /*
        * 1.得到application中的map
        * 2.从request中获取当前客户端的IP地址
        * 3.查看map中是否存在这个IP对应访问次数,如果存在,把次数+1再保存回去
        * 4.如果不存在这个IP,那么说明是第一次访问本站,设置访问次数为1.
        */
        //得到application
        ServletContext app=config.getServletContext();
        Map<String,Integer> map=(Map<String, Integer>) app.getAttribute("map");

        //获取客户端IP地址
        String ip=request.getRemoteAddr();

        //进行判断
        if(map.containsKey(ip)){//这个IP在map中存在,说明不是第一次访问
          int cnt=map.get(ip);
          map.put(ip, cnt+1);
        }else{//这个IP在map中不存在,说明是第一次访问。
        map.put(ip, 1);
        }
        app.setAttribute("map", map);//把map放回APP中
        chain.doFilter(request, response);
      }
      public void destroy() { }

    }

    -----web.xml配置

    <listener>
    <display-name>AListener</display-name>
    <listener-class>cn.itcast.web.listener.AListener</listener-class>
    </listener>

    <filter>
    <filter-name>AFilter</filter-name>
    <filter-class>cn.itcast.web.filter.AFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>AFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    -----show,jsp

    <h1 align="center">显示结果</h1>
    <table align="center" border="1" width="60%">
    <tr>
    <td>IP</td>
    <td>次数</td>
    </tr>
    <c:forEach items="${applicationScope.map }" var="entry">
    <tr>
    <td>${entry.key }</td>
    <td>${entry.value }</td>
    </tr>
    </c:forEach>
    </table>

    ------

    在做这个功能的时候出现的异常:

    1、在Listener类中,方法一定要用contextInitialized(ServletContextEvent sce),要不会抛空指针异常。

  • 相关阅读:
    Quick Union
    Quick Find (QF)
    ubuntu kylin18 安装NVIDIA驱动
    vim 快捷键(update)
    Qt中的ui指针和this指针
    两种状态机扫描按键,第二种只要三行!!!
    RPi:QT+wiringPi demo程序
    esp-12e折腾
    vfd电子时钟制作
    vfd with stm8
  • 原文地址:https://www.cnblogs.com/xiqoqu/p/9187256.html
Copyright © 2011-2022 走看看