filter应用案例一:分IP统计访问次数
统计工作需要在所有资源之前都执行,那么就可以放到Filter中了。
用Map<String,Integer>装载统计的数据。Map创建时间(使用ServletContextListener,在服务器启动时完成创建),Map保存到ServletContext中!!Map需要在Filter中用来保存数据
代码:
1 import java.util.HashMap; 2 import java.util.Map; 3 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletContextEvent; 6 import javax.servlet.ServletContextListener; 7 8 public class MyListener implements ServletContextListener { 9 public void contextInitialized(ServletContextEvent sce) { 10 ServletContext application= sce.getServletContext(); 11 Map<String, Integer>map=new HashMap<String, Integer>(); 12 application.setAttribute("map", map); 13 } 14 15 public void contextDestroyed(ServletContextEvent sce) { 16 } 17 18 }
1 import java.io.IOException; 2 import java.util.Map; 3 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletContext; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 public class CountIPFilter implements Filter { 13 14 private FilterConfig config; 15 public void destroy() {} 16 17 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 18 ServletContext application= config.getServletContext(); 19 Map<String, Integer>map=(Map<String, Integer>) application.getAttribute("map"); 20 String ip=request.getRemoteAddr(); 21 if(map.containsKey(ip)) 22 { 23 int t=map.get(ip); 24 map.put(ip, t+1); 25 } 26 else map.put(ip, 1); 27 chain.doFilter(request, response); 28 } 29 30 public void init(FilterConfig fConfig) throws ServletException { 31 config=fConfig; 32 } 33 34 }
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>My JSP 'show.jsp' starting page</title> 7 <meta http-equiv="pragma" content="no-cache"> 8 <meta http-equiv="cache-control" content="no-cache"> 9 <meta http-equiv="expires" content="0"> 10 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 11 <meta http-equiv="description" content="This is my page"> 12 </head> 13 <body> 14 <h1 align="center">显示结果</h1> 15 <table align="center" width="60%" border="1"> 16 <tr> 17 <th>IP</th> 18 <th>次数</th> 19 </tr> 20 <c:forEach items="${applicationScope.map }" var="entry"> 21 <tr> 22 <td>${entry.key }</td> 23 <td>${entry.value }</td> 24 </tr> 25 </c:forEach> 26 </table> 27 </body> 28 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name></display-name> 4 <welcome-file-list> 5 <welcome-file>index.jsp</welcome-file> 6 </welcome-file-list> 7 <listener> 8 <listener-class>MyListener</listener-class> 9 </listener> 10 <filter> 11 <display-name>CountIPFilter</display-name> 12 <filter-name>CountIPFilter</filter-name> 13 <filter-class>CountIPFilter</filter-class> 14 </filter> 15 <filter-mapping> 16 <filter-name>CountIPFilter</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 </web-app>
运行示例: