统计工作需要在所有资源之前都执行,那么就可以放到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。对比初次访问时间和现在的时间,就可以知道,同一个ip地址在某个时间段内的访问次数,以此来甄别是否是恶意攻击。
把这个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, List<String>> ipMap = (Map<String, List<String>>) context.getAttribute("ipMap");
List<String> listStr = new ArrayList<>();
Date date1 = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date1);
String ip = request.getRemoteAddr();
if(ipMap.containsKey(ip)){
List<String> str= ipMap.get(ip);
for (int i = 0; i <str.size() ; i++) {
String s = str.get(i);
String[] s1 = s.split("@");
String count = s1[1];
listStr.add(dateString+"@"+count+1);
ipMap.put(ip,listStr);
}
}else{
Date date2 = new Date();
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString2 = formatter2.format(date2);
listStr.add(dateString2+"@"+1);
ipMap.put(ip,listStr);
}
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; }