统计工作需要在所有资源之前都执行,那么就可以放到Filter中了。
用Map<String,Integer>装载统计的数据。Map创建时间(使用ServletContextListener,在服务器启动时完成创建),Map保存到ServletContext中!!Map需要在Filter中用来保存数据

代码:

 import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext application= sce.getServletContext();
Map<String, Integer>map=new HashMap<String, Integer>();
application.setAttribute("map", map);
} public void contextDestroyed(ServletContextEvent sce) {
} }

MyListener

 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; public class CountIPFilter implements Filter { private FilterConfig config;
public void destroy() {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletContext application= config.getServletContext();
Map<String, Integer>map=(Map<String, Integer>) application.getAttribute("map");
String ip=request.getRemoteAddr();
if(map.containsKey(ip))
{
int t=map.get(ip);
map.put(ip, t+1);
}
else map.put(ip, 1);
chain.doFilter(request, response);
} public void init(FilterConfig fConfig) throws ServletException {
config=fConfig;
} }

CountIPFilter

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1 align="center">显示结果</h1>
<table align="center" width="60%" border="1">
<tr>
<th>IP</th>
<th>次数</th>
</tr>
<c:forEach items="${applicationScope.map }" var="entry">
<tr>
<td>${entry.key }</td>
<td>${entry.value }</td>
</tr>
</c:forEach>
</table>
</body>
</html>

show.jsp

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>MyListener</listener-class>
</listener>
<filter>
<display-name>CountIPFilter</display-name>
<filter-name>CountIPFilter</filter-name>
<filter-class>CountIPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CountIPFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

web.xml

运行示例:

filter应用案例一:分IP统计访问次数的更多相关文章

  1. 使用javaWeb的二大(Listener、Filter)组件实现分IP统计访问次数

    分析: 统计工作需要在所有资源之前都执行,那么就可以放到Filter中. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计 用什么东西来装载统计的数据.Map<String,Integer ...

  2. Java web--Filter过滤器分IP统计访问次数

    分IP统计访问次数即网站统计每个IP地址访问本网站的次数. 分析 因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访问次数,所以使用过滤器最为方便. 因为需要分IP统计,所以可以在过滤器中创建 ...

  3. 学习笔记_过滤器应用_1(分ip统计网站的访问次数)

    分ip统计网站的访问次数 ip count 192.168.1.111 2 192.168.1.112 59 统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做 ...

  4. 分ip统计网站访问次数

    package web.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; ...

  5. nginx日志中访问最多的100个ip及访问次数

    nginx日志中访问最多的100个ip及访问次数 awk '{print $1}' /opt/software/nginx/logs/access.log| sort | uniq -c | sort ...

  6. Filter和Listener的应用——分IP统计网站访问次数

    一:分析 统计工作需要在所有资源执行前进行,所以需要放在filter中 这个拦截器仅仅进行统计工作,不进行拦截,所以请求必须继续传递下去 用Map<String,integer>来保存数据 ...

  7. [JS12] 统计访问次数

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. iis 限制动态IP地址访问次数

    An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...

  9. Java web 实现 之 Filter分析ip统计网站的访问次数

    统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计的. 用什么东西来装载统计的数据.Map<String,Integer& ...

随机推荐

  1. 用C#读取txt文件的方法

    1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System ...

  2. algorithm 中的常用函数

    非修改性序列操作(12个) 循环         对序列中的每个元素执行某操作         for_each() 查找         在序列中找出某个值的第一次出现的位置         fin ...

  3. KV6.60 SP1

    组态王6.60 SP1全新发布! 组态王6.60 SP1对过去几年6系列中已解决过的故障进行了合并,包括各主线分支.各OEM版本中的故障总计122个,覆盖运行系统.开发系统.历史趋势曲线控件.报表.A ...

  4. VC++ 判断当前系统为32位还是64位

    尝试了在VC++环境下判断系统为32位还是64位的方法,亲测有效!提供的函数如下 BOOL IsWow64() { typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) ...

  5. 【leetcode】Remove Duplicates from Sorted List (easy)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. 声明式事务-整合Spring、Hibernate

    编程式事务:通过编码的方式,让事务处理的代码侵入到核心的业务代码中. 声明式事务:完成了事务处理的代码和业务核心代码的解耦合.提供事务处理代码的复用性和降低维护成本. 声明式事务:aop最典型的应用. ...

  7. 关于each

    1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...

  8. 【转】Java高手真经全套书籍分享

    (转自:http://blog.sina.com.cn/s/blog_4ec2a8390101cd1n.html) 中文名: Java高手真经 原名: JAVA开发专家 作者: 刘中兵Java研究室 ...

  9. hdu 1249 三角形

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...

  10. samba 最简单配置 共享

    [root@GitLab ~]# cat /etc/samba/smb.conf [global] workgroup = WORKGROUP server string = David Samba ...