Filter应用之分ip统计网站的访问次数

ip

count

192.168.1.111

2

192.168.1.112

59

 

因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访问次数,统计工作需要在所有资源之前都执行,所以使用过滤器最为方便。

因为需要分IP统计,所以可以在过滤器中创建一个Map,使用IP为key,访问次数为value。当有用户访问时,获取请求的IP,如果IP在Map中存在,

说明以前访问过,那么在访问次数上加1,即可;IP在Map中不存在,那么设置次数为1。

把这个Map放在ServletContextListener,在服务器启动时完成创建(使用监听器),并只在到ServletContext中)。

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

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

代码

index.jsp

  <body>

<h1>IP统计访问次数</h1>

<table align="center" width="50%" border="1">

    <tr>

       <th>IP地址</th>

       <th>次数</th>

    </tr>

<c:forEach items="${applicationScope.ipCountMap }" var="entry">

    <tr>

       <td>${entry.key }</td>

       <td>${entry.value }</td>

    </tr>

</c:forEach>

 </table>

  </body>

 

IPFilter

publicclass IPFilter implements Filter {

    private ServletContext context;

 

    publicvoid init(FilterConfig fConfig) throws ServletException {

       context = fConfig.getServletContext();

       Map<String, Integer> ipCountMap = Collections

              .synchronizedMap(new LinkedHashMap<String, Integer>());

        context.setAttribute("ipCountMap", ipCountMap);

    }

 

    @SuppressWarnings("unchecked")

    publicvoid doFilter(ServletRequest request, ServletResponse response,

           FilterChain chain) throws IOException, ServletException {

       HttpServletRequest req = (HttpServletRequest) request;

       String ip = req.getRemoteAddr();

 

       Map<String, Integer> ipCountMap = (Map<String, Integer>) context

              .getAttribute("ipCountMap");

       Integer count = ipCountMap.get(ip);

       if (count == null) {

           count = 1;

        } else {

           count += 1;

       }

       ipCountMap.put(ip, count);

 

       context.setAttribute("ipCountMap", ipCountMap);

       chain.doFilter(request, response);

    }

 

    publicvoid destroy() {}

}

  <filter>

    <display-name>IPFilter</display-name>

    <filter-name>IPFilter</filter-name>

    <filter-class>cn.itcast.filter.ip.IPFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>IPFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值