ServletContextListenerTest 类
设置监听器监听
package com.zhiyou.ipadr.demo;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletContextListenerTest implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//在ServletContextListener的init方法中创建map
Map<String,Integer> map=new HashMap<>();
//获取servletContext
ServletContext context = sce.getServletContext();
context.setAttribute("mapIP", map);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
IPCountFilter类
package com.zhiyou.ipadr.demo;
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;
@WebFilter(urlPatterns="/*")
public class IPCountFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//分类统计IP访问次数
ServletContext context = request.getServletContext();
Map<String,Integer> map = (Map<String,Integer>)context.getAttribute("mapIP");
//获取请求的IP地址
String ip = request.getRemoteAddr();
//判断map 中是否有该IP的访问次数
if(map.containsKey(ip)){
map.put(ip, map.get(ip)+1);
}else{
map.put(ip, 1);
}
//重新放入ServletContext
context.setAttribute("mapIP", map);
System.out.println(map.get(ip));
//放行
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
index.jsp
http://localhost:8080/week09_day04_3/index.jsp
多刷新几次,几次即为网站访问量,因为监听器的注释为@WebFilter(urlPatterns=”/*”),即监听所有的动作。刷新的次数就是网站访问量
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
show.jsp
http://localhost:8080/week09_day04_3/show.jsp
表格展示ip和显示次数
<%@ page language="java" contentType="text/html; charset=UTF-8"
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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>分类统计IP访问次数</h3>
<table width="300px" align="center" border="1" cellspacing="0" bordercolor="blue">
<tr>
<th>IP</th>
<th>访问次数</th>
</tr>
<c:forEach items="${applicationScope.mapIP }" var="obj" varStatus="vs">
<tr>
<td>${obj.key }</td>
<td>${obj.value }</td>
</tr>
</c:forEach>
</table>
</body>
</html>