监听器
package cn.itcast.web.listener; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class AListener implements ServletContextListener { /** * 在服务器启动时创建Map,保存到ServletContext */ 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); } public void contextDestroyed(ServletContextEvent sce) { } }
过滤器
package cn.itcast.web.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 cxf * */ public class AFilter implements Filter { private FilterConfig config; public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { /* * 1. 得到application中的map * 2. 从request中获取当前客户端的ip地址 * 3. 查看map中是否存在这个ip对应访问次数,如果存在,把次数+1再保存回去 * 4. 如果不存在这个ip,那么说明是第一次访问本站,设置访问次数为1 */ /* * 1. 得到appliction */ ServletContext app = config.getServletContext(); Map<String,Integer> map = (Map<String, Integer>) app.getAttribute("map"); /* * 2. 获取客户端的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; } }
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" 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> <filter> <display-name>AFilter</display-name> <filter-name>AFilter</filter-name> <filter-class>cn.itcast.web.filter.AFilter</filter-class> </filter> <filter-mapping> <filter-name>AFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>cn.itcast.web.listener.AListener</listener-class> </listener> </web-app>
页面
<%@ 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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </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>
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1728685