統計工作需要在所有資源之前都執行,那么就可以放到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"