filter:分IP统计次数

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

过滤器不打算做拦截操作,因为只是用来做统计。

用什么东西来装载统计数据。Map<String.integer>

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

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

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

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

代码:

show.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">显示结果</h1>
<table align="center" width="60%" border="1">
   <tr>
      <td>IP</td>
      <td>次数</td>
   </tr>
<c:forEach items="${applicationScope.map}" var="entry">
   <tr >
      <td>${entry.key}</td>
      <td>${entry.value}</td>
   </tr>
  </c:forEach>
</table>
</body>
</html>

 Afilter.jsp

package com.tinstu.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;
import javax.servlet.annotation.WebFilter;

public class Afilter implements Filter {
	private FilterConfig config;


	public void destroy() {
	
	}

	//从application中获取app
	//从request中得到当前的客户端IP
	//进行统计工作。=,结果保存到Map
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		//1.从application中获取app
		//2.从request中得到当前的客户端IP
		//查看map中是否存在这个IP对应的访问次数,如果存在,把次数+1再保存回去
		//4.如果不存在这个IP,那么说明时第一次访问本站,设置访问 次数为1
		
		//1.从application中获取app
		ServletContext app = config.getServletContext();
		Map<String,Integer> map = (Map<String, Integer>) app.getAttribute("map");
		//获取客户端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;
	}

}

 listener.jsp

package com.tinstu.listener;

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

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

public class AListener implements ServletContextListener {


  
    public void contextDestroyed(ServletContextEvent sce)  { 

    } 

  //在服务器启动时,创建map,保存到SerevletContextListener
    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);
    	
 
    }
	
}

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>IPtongji</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
  <listener-class>com.tinstu.listener.AListener</listener-class>
  </listener>
<filter>
   <filter-name>Afilter</filter-name>
   <filter-class>com.tinstu.filter.Afilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>Afilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 注意:

1.xml中filter和listener都要进行配置

2.主要filter和listener的方法名,哪个是启动,哪个是关闭!!

IP


1、说明:

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

 

2、分析:

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

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

把这个Map存放到ServietContext中!

阅读剩余
THE END