Java web 實現 之 Filter分析ip統計網站的訪問次數

來源:互聯網
上載者:User

標籤:https   client   xmlns   oct   equals   res   href   copy   hashmap   

統計工作需要在所有資源之前都執行,那麼就可以放到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等反向 Proxy軟體就不能擷取到用戶端的真實IP地址了。如果使用了反向 Proxy軟體,將http://192.168.1.110:2046/ 的URL反向 Proxy為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檔案,而是先由Proxy 伺服器去訪問http://192.168.1.110:2046/index.jsp ,Proxy 伺服器再將訪問到的結果返回給我們的瀏覽器,因為是Proxy 伺服器去訪問index.jsp的,所以index.jsp中通過request.getRemoteAddr() 的方法擷取的IP實際上是Proxy 伺服器的地址,並不是用戶端的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 urllib2import 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"
 

Java web 實現 之 Filter分析ip統計網站的訪問次數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.