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

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

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

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

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

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

  • Map需要在Filter中用来保存数据
  • Map需要在页面使用,打印Map中的数据

1 分析

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

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

把这个Map存放到ServletContext中!

2 代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.cug.filter02.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>com.cug.filter02.MyListener</listener-class>
</listener> </web-app>
package com.cug.filter02;

import java.util.LinkedHashMap;
import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener{ @Override
public void contextDestroyed(ServletContextEvent arg0) { } @Override
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
Map<String, Integer> ipMap = new LinkedHashMap<String, Integer>();
context.setAttribute("ipMap", ipMap);
} } package com.cug.filter02; 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 MyFilter implements Filter{ private FilterConfig filterConfig; @Override
public void destroy() { } @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletContext context = filterConfig.getServletContext();
Map<String, Integer> ipMap = (Map<String, Integer>) context.getAttribute("ipMap");
String ip = request.getRemoteAddr();
if(ipMap.containsKey(ip)){
Integer count = ipMap.get(ip);
ipMap.put(ip,count+1);
}else{
ipMap.put(ip,1);
}
context.setAttribute("ipMap", ipMap);
chain.doFilter(request, response);
} @Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<table align="center" width="60%" border="1">
<tr>
<th>ip</th>
<th>count</th>
</tr>
<c:forEach items="${applicationScope.ipMap}" var="entry">
<tr>
<td>${entry.key }</td>
<td>${entry.value }</td>
</tr>
</c:forEach>
</table>
</body>
</html>

注意:

在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr() ,这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。
如果使用了反向代理软件,将http://192.168.1.110:2046/ 的URL反向代理为http://www.xxx.com/ 的URL时,用request.getRemoteAddr() 方法获取的IP地址是:127.0.0.1 或 192.168.1.110 ,而并不是客户端的真实IP。
经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。当我们访问http://www.xxx.com/index.jsp/ 时,其实并不是我们浏览器真正访问到了服务器上的index.jsp文件,而是先由代理服务器去访问http://192.168.1.110:2046/index.jsp ,代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问index.jsp的,所以index.jsp中通过request.getRemoteAddr() 的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。于是可得出获得客户端真实IP地址的方法:
     public  String getIpAddr(HttpServletRequest request)  {
String ip = request.getHeader( " x-forwarded-for " );
if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) {
ip = request.getHeader( " Proxy-Client-IP " );
}
if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) {
ip = request.getHeader( " WL-Proxy-Client-IP " );
}
if (ip == null || ip.length() == 0 || " unknown " .equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}

补充:最后后台可以执行一段python,完成对访问地址的统计和分析:

不完整代码
#-*- coding:gbk -*-
import urllib2
import re url = "http://www.ip138.com/ips138.asp?ip=%s&action=2" % ipaddr
u = urllib2.urlopen(url)
s = u.read()
#Get IP Address
ip = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',s)
print "\n****** Below Result From IP138 Database *****"
print "IP Address:",ip[0]
#Get IP Address Location
result = re.findall(r'(<li>.*?</li>)',s)
for i in result:
print i[4:-5]
print "*"*45
print "\n"
上一篇:jboss部署出现jboss.naming.context.java.rmi找不到错误


下一篇:Windows下配置nginx+FastCgi + Spawn-fcgi