您的位置:首页 > 运维架构 > 网站架构

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

2015-07-12 14:00 711 查看

分ip统计网站的访问次数

ip

count

192.168.1.111

2

192.168.1.112

59

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

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

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

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

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

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

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

1 说明
  网站统计每个IP地址访问本网站的次数。

2 分析
  因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访问次数,所以使用过滤器最为方便。

  因为需要分IP统计,所以可以在过滤器中创建一个Map,使用IP为key,访问次数为value。当有用户访问时,获取请求的IP,如果IP在Map中存在,说明以前访问过,那么在访问次数上加1,即可;IP在Map中不存在,那么设置次数为1。

把这个Map存放到ServletContext中!

3 代码
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>

[崔1] </table>

</body>

IPFilter

public class IPFilter implements Filter {

private ServletContext context;

public void init(FilterConfig fConfig) throws ServletException {

context = fConfig.getServletContext(); //[崔2] 保存ServletContext

Map<String, Integer> ipCountMap = Collections

.synchronizedMap(new LinkedHashMap<String, Integer>());[崔3] 创建一个Map,保存到ServletContext中

context.setAttribute("ipCountMap", ipCountMap);

}

@SuppressWarnings("unchecked")

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

String ip = req.getRemoteAddr();[崔4]

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

.getAttribute("ipCountMap");

[崔5]

Integer count = ipCountMap.get(ip);[崔6]

if (count == null) {

count = 1;

[崔7] } else {

count += 1;[崔8] 否则在原有次数上加1

}

ipCountMap.put(ip, count);[崔9]把ip和次数设置到map中

context.setAttribute("ipCountMap", ipCountMap);[崔10] 把map存放到context中

chain.doFilter(request, response);[崔11] 放行!

}

public void 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]循环遍历在ServletContext中的map,其中key是ip地址,value是访问次数

[崔2]保存ServletContext

[崔3]创建一个Map,保存到ServletContext中

[崔4]获取请求方的ip

[崔5]在context中获取Map

[崔6]在Map中获取当前ip的访问次数

[崔7]如果这个ip在map中不存在,那么设置访问次数为1

[崔8]否则在原有次数上加1

[崔9]把ip和次数设置到map中

[崔10]把map存放到context中

[崔11]放行!

完整代码:

AFilter

package cn.itcast.web.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中
* @author cxf
*
*/
public class AFilter implements Filter {
private FilterConfig config;
public void destroy() {
}

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
*/
/*
* 1. 得到appliction
*/
ServletContext app = config.getServletContext();
Map<String,Integer> map = (Map<String, Integer>) app.getAttribute("map");
/*
* 2. 获取客户端的ip地址
*/
String ip = request.getRemoteAddr();
/*
* 3. 进行判断
*/
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 init(FilterConfig fConfig) throws ServletException {
this.config = fConfig;
}
}


Alistener

package cn.itcast.web.listener;

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

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

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);  //取名为map,也可以为任意其他
}

public void contextDestroyed(ServletContextEvent sce) {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: