统计工作需要在所有资源之前都执行,那么就可以放到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. 关于TortoiseSVN的一些知识

    TortoiseSVN的一切命令都是相对于它自己来说的 1.Import,导入,指的是导入SVN的代码库,即Repository. 2.Export,导出,指的是将代码从Repository中导出到你 ...

  2. Excel保护工作表

    最近见到一个Excel文件,其中的表不能选中,不能编辑,今天研究了一下 明白了其设置方法 excel中开始--格式--保护工作表--去掉所有勾选,输入保护密码  即可

  3. 用基础动画实现iOS控件循环旋转

    - (void)viewDidLoad { [super viewDidLoad]; UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(sc ...

  4. diff & pattch 命令

    基础知识 该命令的功能为逐行比较两个文本文件,列出其不同之处.它比comm命令完成更复杂的检查.它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序. 语法:diff ...

  5. sql server 导出表结构

    今天准备整理下手里面几个数据库,形成一个表结构文档,方便以后维护使用. 网上找到一个脚本还不错,小小的修改就满足了我的要求,执行完SQL脚本. 在结果就能看到数据库所有表的结构,这个时候只要全选,然后 ...

  6. mybatis参数错误 Parameter '×××' not found. Available parameters are [0, 1, param1, param2]

    报错的代码 @Update("update staff_info set ApplyState = #{applyState} where Id = #{userId}") int ...

  7. 【QT】C++ GUI Qt4 学习笔记1

    Find对话框实现 平台 Qt5.3.2 MinGW4.8.2 注意创建时用QDialog finddialog.h #ifndef FINDDIALOG_H #define FINDDIALOG_H ...

  8. validation验证器指定action中某些方法不需要验证

    今天写代码时,遇到个问题,在一个输入数据的页面有一个按钮,单击会发出请求从数据库中取数据,在这里出现问题,单击该按钮,配置的validation起作用,该请求没有到达后台的action 点击按钮选择作 ...

  9. ios电话/密码/验证码/身份证的正则表达式

    // 一 .电话号码正则表达式 -(BOOL)testPhoneNumber:(NSString *)text { NSString *regex =@"(13[0-9]|0[1-9]|0[ ...

  10. css3属性flex弹性布局设置三列(四列)分布样式

    参考:阮一峰的网络日志 <!doctype html> <html lang="en"> <head> <meta charset=&qu ...