可以肯定的是,加载顺序与它们在 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