04-过滤器与监听器- ( 二 ) 综合其中ip访问次数的实例及遇到的问题和解决办法

06- 练习(一)分ip统计各自的访问次数(出现问题及解决办法);

这个实验,和前面的统计网站访问实验类似,不过,多了统计ip,各个ip的访问次数;这一开始就出现两个问题我也是郁闷;

当然这个实验首先要知道,可以借助map 存储访问的数据;(在服务器启动时创建ServletContextListener)什么时候创建,保存在哪里?(servletContext中);

先抛出我出现问题的源码叭:

<!-- listener -->
<listener>
    <listener-class>Listener.ip_vis_count_Listener</listener-class>
</listener>
<!-- filter -->
<filter>
    <filter-name>ip_vis_count_Filter</filter-name>
    <filter-class>Filter.ip_vis_count_Filter</filter-class>
</filter>
<filter-mapping>
	<filter-name>ip_vis_count_Filter</filter-name>
    <url-pattern>Filter.ip_vis_count_Filter</url-pattern>
</filter-mapping>
// java.Listener
package Listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.LinkedHashMap;
import java.util.Map;


public class ip_vis_count_Listener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        Map<String,Integer> map = new LinkedHashMap<>();
        ServletContext application = sce.getServletContext();
        application.setAttribute("map",map);//
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }
}


// java.filter
package Filter;

import javax.servlet.*;
import java.io.IOException;
import java.util.Map;

public class ip_vis_count_Filter implements Filter {
    private FilterConfig config;
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        ServletContext app = config.getServletContext();
        //
        Map<String,Integer> map = (Map<String, Integer>)app.getAttribute("map");
        /* get the user ip: */
        String ip = req.getRemoteAddr();
        if(map.containsKey(ip)){
            int cnt = map.get(ip);
            map.put(ip,cnt+1);
        }else {
            map.put(ip,1);
        }
        app.setAttribute("map",map);

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

}

以上是未被检查的代码:

01- map类型的强转换,

在这里插入图片描述

如果没有上面的@SuppressWarnings("unchecked")部分,就会报错,

02- Error during artifact deployment. See server log for details.

这个我也是无语了,找了好久,到怀疑人生,最后发现,是自己filter 的配置地方写错了

在这里插入图片描述

03- jsp中 <c:forEach></c:forEach>;

这个错还是很好发现,平时看到foreach比较习惯点,也差点没找出来;

<!-- 这里是jsp 调试代码: -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2021/6/21
  Time: 17:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ip_vis_count</title>
</head>
<body>
<h1>the ip counts:</h1>
<hr/>
<table align="center" width="60%" border="1">
    <tr>
        <th>IP</th>
        <th>counts</th>
    </tr>
<%--    forEach 中,E 大写,不是 小写的e,我裂开--%>
    <c:forEach items="${ applicationScope.map }" var="entry">
        <tr>
            <td>${ entry.key }</td>
            <td>${ entry.value }</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

04- 最后附上 效果结果:

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

J.CH.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值