filter应用案例一:分IP统计访问次数
统计工作需要在所有资源之前都执行,那么就可以放到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统计访问次数的更多相关文章
- 使用javaWeb的二大(Listener、Filter)组件实现分IP统计访问次数
分析: 统计工作需要在所有资源之前都执行,那么就可以放到Filter中. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计 用什么东西来装载统计的数据.Map<String,Integer ...
- Java web--Filter过滤器分IP统计访问次数
分IP统计访问次数即网站统计每个IP地址访问本网站的次数. 分析 因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访问次数,所以使用过滤器最为方便. 因为需要分IP统计,所以可以在过滤器中创建 ...
- 学习笔记_过滤器应用_1(分ip统计网站的访问次数)
分ip统计网站的访问次数 ip count 192.168.1.111 2 192.168.1.112 59 统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做 ...
- 分ip统计网站访问次数
package web.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; ...
- nginx日志中访问最多的100个ip及访问次数
nginx日志中访问最多的100个ip及访问次数 awk '{print $1}' /opt/software/nginx/logs/access.log| sort | uniq -c | sort ...
- Filter和Listener的应用——分IP统计网站访问次数
一:分析 统计工作需要在所有资源执行前进行,所以需要放在filter中 这个拦截器仅仅进行统计工作,不进行拦截,所以请求必须继续传递下去 用Map<String,integer>来保存数据 ...
- [JS12] 统计访问次数
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- iis 限制动态IP地址访问次数
An IP Address Blocking HttpModule for ASP.NET in 9 minutes namespace YourModuleNameHere 10 { 11 publ ...
- Java web 实现 之 Filter分析ip统计网站的访问次数
统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计的. 用什么东西来装载统计的数据.Map<String,Integer& ...
随机推荐
- perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误
今天写脚本遇到Can't use string ("bond2 Link encap:InfiniBand ") as a symbol ref while "s ...
- 3.kvm的基本管理
1.查看KVM虚拟机配置文件 #KVM虚拟机默认配置文件位置 [root@kvm qemu]# pwd /etc/libvirt/qemu [root@kvm qemu]# ll total 12 # ...
- HDU 3111 Sudoku(精确覆盖)
数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...
- (译)利用ASP.NET加密和解密Web.config中连接字符串
介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, A ...
- C#集合及特殊字符
集合里面 打印 出来时 要把 集合内的格式转化为其他格式! 壹. 集合 在.add之前 为空 数组 同样 添加元素之前 为 空(下一章超市购物例题具体体现) 1.System Collec ...
- mybatis一对多查询
18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher ...
- iOS开发MAC下配置Svn和Git
如果你对iOS开发中的版本控制还不了解那么你可以先看看这篇(大致看一遍就ok) http://www.cnblogs.com/iCocos/p/4767692.html 关于版本控制使用起来并不难 ...
- mac os 下打开FTP服务器
mac下一般用smb服务来进行远程文件访问,但要用FTP的话,高版本的mac os默认关掉了,可以用如下命令打开: sudo -s launchctl load -w /System/Library/ ...
- velocity思维导图笔记
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
参考地址:http://www.cnblogs.com/zhili/p/4039111.html 一.如何在Windows Services中寄宿WCF服务 第一步:创建Windows 服务项目,具体 ...