统计访问当前网站的ip地址及其访问次数,并在页面显示。
方法很简单,就是在servletContext中使用一个map对象来存放ip地址,以及该ip地址的访问次数。
新建一个AFilter类实现Filter接口,然后在 AFilter类的doFilter中统计当前访问的ip地址访问次数。
然后在show.jsp页面显示统计的结果。
1.在listener中创建一个Map对象来保存。
因为LinkedHashMap是HashMap的一个子类,它保留插入的顺序,如果需要输出的顺序和输入时的相同,那么就选用LinkedHashMap。此处就选择使用LinkedHashMap来存储数据。
package com.aslan.listener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class AListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sec) {
/*
* 服务器启动时创建Map,保存到ServletContext
* */
Map<String, Integer> map = new LinkedHashMap<String,Integer>();
//得到servletContext
ServletContext application = sec.getServletContext();
application.setAttribute("map", map);
System.out.println("in listener");
}
public void contextDestroyed(ServletContextEvent arg0) {
}
}
2.创建一个过滤器AFilter来统计ip地址的访问次数
package com.aslan.filter;
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;
/**
* Servlet Filter implementation class AFilter
*/
public class AFilter implements Filter {
private FilterConfig config;
/**
* Default constructor.
*/
public AFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("in filer");
//1.得到application中的map
ServletContext application = config.getServletContext();
//2.从request中获取当前用户的ip地址
String ip = request.getLocalAddr();
//3.查看map中是否存在这个ip对应的访问次数,如果存在把次数+1,再保存回去
@SuppressWarnings("unchecked")
Map<String, Integer> map = (Map<String, Integer>) application.getAttribute("map");
System.out.println(map);
if (map.containsKey(ip)) {
map.put(ip, map.get(ip)+1);
}
else {
map.put(ip,1);
}
System.out.println(ip);
System.out.println(map);
//4.如果不存在这个ip,说明是第一次访问本站,设置访问次数为1
application.setAttribute("map", map);
//方形
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init filer");
this.config = fConfig;
}
}
3.使用一个show.jsp来显示结果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table align="center" border="1" width="60%">
<tr>
<th>Ip </th>
<th>次数 </th>
</tr>
<c:forEach items="${applicationScope.map}" var="entry">
<tr>
<th>${entry.key } </th>
<th>${entry.value } </th>
</tr>
</c:forEach>
</table>
</body>
</html>
4.web.xml的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.aslan.listener.AListener</listener-class>
</listener>
<filter>
<filter-name>AFilter</filter-name>
<filter-class>com.aslan.filter.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
上面4个步骤就实现了网站对IP地址访问次数的统计。
。