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>