分IP统计访问次数(监听器负责创建map,过滤器负责统计,JSP负责显示结果)

可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。

最终得出的结论是:listener -> filter -> servlet

1是Listener中创建map

package cn.etc.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;

@WebListener
public class AListener implements ServletContextListener {

	/**
	 * 在服务器启动时创建Map,保存到ServletContext
	 */
    public void contextInitialized(ServletContextEvent event)  { 
    	//创建Map
    	Map<String, Integer> map = new LinkedHashMap<String, Integer>();
    	//得到ServletContext
    	ServletContext application = event.getServletContext();
    	//把map保存到application中
    	application.setAttribute("map", map);
    }
	
    public void contextDestroyed(ServletContextEvent event)  { 
    }
}

listener在web.xml中的配置:

<listener>

    <listener-class>cn.edu.AListener</listener-class>

</listener>

但是测试的时候不对在web.xml里面配置监听器,我是在Servlet里面配置的

2是Filter中做统计

package cn.edu.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;

/**
 * 从application中获取Map
 * 从request中得到当前客户端的IP
 * 进行统计工作,结果保存到Map中
 * @author pp
 *
 */
public class IpCountFilter implements Filter {

	private FilterConfig config;//config.getServletContext();//获取上下文             

	/**
	 * 在服务器启动时就会执行本方法,而且本方法只执行一次!
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		/**
		 * 1.得到application中的map
		 * 2.从request中获取当前客户端的IP地址
		 * 3.查看map中是否存在这个ip对应访问次数,如果存在,把次数+1再保存到map中
		 * 4.如果不存在这个Ip,那么说明是第一次访问本站,设置方位次数为1
		 */
		//1.得到application
		ServletContext application = config.getServletContext();//获取上下文                    
		//从application中把map拿出来	  这个map是在AListener监听器里面											   map
		Map<String,Integer> map = (Map<String, Integer>)application.getAttribute("map");
		//2.获取客户端的ip地址
		String ip = request.getRemoteAddr();//不用强转为HttpServletRequest,因为getRemoteAddr就在ServletRequest中
		//3.进行判断
		if(map.containsKey(ip)){//这个ip在map中存在,说明不是第一次访问
			int count = map.get(ip);//通过键ip获得对应的值count
			count++;//加1
			map.put(ip, count);//把加1后的count和ip存入map中
		} else{//这个ip在map中不存在,说明是第一次访问
			map.put(ip, 1);//第一次访问的ip记为1了
		}
		
		//application.setAttribute("map", map);//可不存?! 引用!再把map放回到application中
		chain.doFilter(request, response);//放行
	}

	public void destroy() {
		//
	}

}

Filter在web.xml里面的配置:

  <filter>
    <filter-name>ipCount</filter-name>
    <filter-class>cn.edu.filter.IpCountFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>ipCount</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  


3是show.jsp中打印结果

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>统计网站访问量</title>
</head>
<body>
<h1 align="center">分IP统计访问次数</h1>
<table align="center" width="60%" border="1">
	<tr>
		<th>IP地址</th>
		<th>访 问 量</th>
	</tr>
	<!--循环遍历在ServletContext中的map,其中key是ip地址,value是访问次数 -->
	<c:forEach items="${applicationScope.map }" var="entry">
		<tr>
			<td>${entry.key }</td>
			<td>${entry.value }</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>


那个0:0:0:0....是localhost访问的,127.0.0.1是127.0.0.1访问的,不一样的ip


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝地反击T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值