1. package web.listener;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5. import javax.servlet.annotation.WebListener;
  6. import javax.servlet.http.HttpSessionAttributeListener;
  7. import javax.servlet.http.HttpSessionListener;
  8. import java.util.LinkedHashMap;
  9. import java.util.Map;
  10. @WebListener()
  11. public class AListener implements ServletContextListener,
  12. HttpSessionListener, HttpSessionAttributeListener {
  13. /*
  14. * 在服务启动时创建Map,保存到ServletContext
  15. * */
  16. public void contextInitialized(ServletContextEvent sce) {
  17. //创建Map
  18. Map<String,Integer> map = new LinkedHashMap<String, Integer>();
  19. //得到ServletContext
  20. ServletContext application = sce.getServletContext();
  21. application.setAttribute("map",map);
  22. }
  23. public void contextDestroyed(ServletContextEvent sce) {
  24. }
  25. }
  1. 1 package web.filter;
  2. 2
  3. 3 import javax.servlet.*;
  4. 4 import javax.servlet.annotation.WebFilter;
  5. 5 import java.io.IOException;
  6. 6 import java.util.Map;
  7. 7
  8. 8 /*
  9. 9 * 从application中获取Map
  10. 10 * 从request中得到当前客户端的IP
  11. 11 * 进行统计工作,结果保存到Map中
  12. 12 * */
  13. 13 @WebFilter(filterName = "AFilter",urlPatterns = "/*")
  14. 14 public class AFilter implements Filter {
  15. 15 private FilterConfig config;
  16. 16 public void destroy() {
  17. 17 }
  18. 18
  19. 19 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
  20. 20 /*
  21. 21 * 1、得到application中的map
  22. 22 * 2、从request中获取当前客户端的IP地址
  23. 23 * 3、查看map中是否存在这个IP对应访问次数,如果存在,把次数+1再保存回去
  24. 24 * 4、如果不存在这个IP,那么说明是第一次访问本站,设置访问次数为1
  25. 25 * */
  26. 26 /*
  27. 27 * 1、得到application
  28. 28 * */
  29. 29 ServletContext app = config.getServletContext();
  30. 30 Map<String,Integer> map = (Map<String,Integer>) app.getAttribute("map");
  31. 31 /*
  32. 32 * 2、得到客户端的ip地址
  33. 33 * */
  34. 34 String ip = req.getRemoteAddr();
  35. 35 /*
  36. 36 *3、进行判断
  37. 37 * */
  38. 38 if (map.containsKey(ip)) {
  39. 39 int cnt = map.get(ip);
  40. 40 map.put(ip,cnt+1);
  41. 41 }else {
  42. 42 map.put(ip,1);
  43. 43 }
  44. 44 app.setAttribute("map",map);
  45. 45
  46. 46 chain.doFilter(req, resp);//肯定放行
  47. 47 }
  48. 48 /*
  49. 49 * 在服务器启动时就会执行本方法,而且本方法只执行一次
  50. 50 * */
  51. 51 public void init(FilterConfig config) throws ServletException {
  52. 52 this.config= config;
  53. 53 }
  54. 54 }
  1. 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. 2 <%--
  3. 3 Created by IntelliJ IDEA.
  4. 4 User: Mac
  5. 5 Date: 13/09/2017
  6. 6 Time: 12:37 PM
  7. 7 To change this template use File | Settings | File Templates.
  8. 8 --%>
  9. 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  10. 10 <html>
  11. 11 <head>
  12. 12 <title>Title</title>
  13. 13 </head>
  14. 14 <body>
  15. 15 <h1>显示结果</h1>
  16. 16 <table align="center" width="60%" border="1">
  17. 17 <tr>
  18. 18 <td>ip</td>
  19. 19 <td>次数</td>
  20. 20 </tr>
  21. 21 <c:forEach items="${applicationScope.map}" var="entry">
  22. 22 <tr>
  23. 23 <td>${entry.key}</td>
  24. 24 <td>${entry.value}</td>
  25. 25 </tr>
  26. 26 </c:forEach>
  27. 27 </table>
  28. 28 </body>
  29. 29 </html>

 

版权声明:本文为gdwkong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gdwkong/p/7635545.html