监听器和过滤器写的分类统计IP访问次数

这里写图片描述

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值