统计工作需要在所有资源之前都执行,那么就可以放到Filter中了。
用Map<String,Integer>装载统计的数据。Map创建时间(使用ServletContextListener,在服务器启动时完成创建),Map保存到ServletContext中!!Map需要在Filter中用来保存数据
代码:
import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext application= sce.getServletContext();
Map<String, Integer>map=new HashMap<String, Integer>();
application.setAttribute("map", map);
} public void contextDestroyed(ServletContextEvent sce) {
} }
MyListener
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 CountIPFilter implements Filter { private FilterConfig config;
public void destroy() {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletContext application= config.getServletContext();
Map<String, Integer>map=(Map<String, Integer>) application.getAttribute("map");
String ip=request.getRemoteAddr();
if(map.containsKey(ip))
{
int t=map.get(ip);
map.put(ip, t+1);
}
else map.put(ip, 1);
chain.doFilter(request, response);
} public void init(FilterConfig fConfig) throws ServletException {
config=fConfig;
} }
CountIPFilter
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
</head>
<body>
<h1 align="center">显示结果</h1>
<table align="center" width="60%" border="1">
<tr>
<th>IP</th>
<th>次数</th>
</tr>
<c:forEach items="${applicationScope.map }" var="entry">
<tr>
<td>${entry.key }</td>
<td>${entry.value }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
show.jsp
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>MyListener</listener-class>
</listener>
<filter>
<display-name>CountIPFilter</display-name>
<filter-name>CountIPFilter</filter-name>
<filter-class>CountIPFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CountIPFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
web.xml
运行示例: