Java Servelet 做一个简单的分IP访问统计系统
实现流程:
代码实现
AListener.java,监听,启动tomcat就创建map集合
package cn.alone88;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener()
public class AListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
// Public constructor is required by servlet spec
public AListener() {
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
ServletContext application = sce.getServletContext();
application.setAttribute("map", map);
}
public void contextDestroyed(ServletContextEvent sce) {
}
// -------------------------------------------------------
// HttpSessionListener implementation
// -------------------------------------------------------
public void sessionCreated(HttpSessionEvent se) {
}
public void sessionDestroyed(HttpSessionEvent se) {
}
// -------------------------------------------------------
// HttpSessionAttributeListener implementation
// -------------------------------------------------------
public void attributeAdded(HttpSessionBindingEvent sbe) {
}
public void attributeRemoved(HttpSessionBindingEvent sbe) {
}
public void attributeReplaced(HttpSessionBindingEvent sbe) {
}
}
AFilter.java 拦截器,拦截IP,写入Map
package cn.alone88;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "AFilter")
public class AFilter implements Filter {
private ServletContext config;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//得到ServletContext map
// 得到IP
// 查看map是否存在ip
ServletContext app = this.config;
//获取Map
Map<String, Integer> map = (Map<String, Integer>)app.getAttribute("map");
// 获取IP
String ip = req.getRemoteAddr();
//判断IP是否已存在
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.getServletContext();
}
}
Ip.jsp 页面展示 ,通过c:forEach 遍历集合
<%@ page import="java.util.Map" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<th>Ip</th>
<th>次数</th>
</tr>
<c:forEach items="${ applicationScope.map }" var="entry">
<tr>
<td>${entry.key}</td>
<td>${entry.value}</td>
</tr>
</c:forEach>
</table>
</body>
</html>