package web.listener;

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.HttpSessionListener;
import java.util.LinkedHashMap;
import java.util.Map; @WebListener()
public class AListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
/*
* 在服务启动时创建Map,保存到ServletContext
* */
public void contextInitialized(ServletContextEvent sce) {
//创建Map
Map<String,Integer> map = new LinkedHashMap<String, Integer>();
//得到ServletContext
ServletContext application = sce.getServletContext();
application.setAttribute("map",map);
}
public void contextDestroyed(ServletContextEvent sce) { }
}
 1 package web.filter;
2
3 import javax.servlet.*;
4 import javax.servlet.annotation.WebFilter;
5 import java.io.IOException;
6 import java.util.Map;
7
8 /*
9 * 从application中获取Map
10 * 从request中得到当前客户端的IP
11 * 进行统计工作,结果保存到Map中
12 * */
13 @WebFilter(filterName = "AFilter",urlPatterns = "/*")
14 public class AFilter implements Filter {
15 private FilterConfig config;
16 public void destroy() {
17 }
18
19 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
20 /*
21 * 1、得到application中的map
22 * 2、从request中获取当前客户端的IP地址
23 * 3、查看map中是否存在这个IP对应访问次数,如果存在,把次数+1再保存回去
24 * 4、如果不存在这个IP,那么说明是第一次访问本站,设置访问次数为1
25 * */
26 /*
27 * 1、得到application
28 * */
29 ServletContext app = config.getServletContext();
30 Map<String,Integer> map = (Map<String,Integer>) app.getAttribute("map");
31 /*
32 * 2、得到客户端的ip地址
33 * */
34 String ip = req.getRemoteAddr();
35 /*
36 *3、进行判断
37 * */
38 if (map.containsKey(ip)) {
39 int cnt = map.get(ip);
40 map.put(ip,cnt+1);
41 }else {
42 map.put(ip,1);
43 }
44 app.setAttribute("map",map);
45
46 chain.doFilter(req, resp);//肯定放行
47 }
48 /*
49 * 在服务器启动时就会执行本方法,而且本方法只执行一次
50 * */
51 public void init(FilterConfig config) throws ServletException {
52 this.config= config;
53 }
54 }
 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2 <%--
3 Created by IntelliJ IDEA.
4 User: Mac
5 Date: 13/09/2017
6 Time: 12:37 PM
7 To change this template use File | Settings | File Templates.
8 --%>
9 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
10 <html>
11 <head>
12 <title>Title</title>
13 </head>
14 <body>
15 <h1>显示结果</h1>
16 <table align="center" width="60%" border="1">
17 <tr>
18 <td>ip</td>
19 <td>次数</td>
20 </tr>
21 <c:forEach items="${applicationScope.map}" var="entry">
22 <tr>
23 <td>${entry.key}</td>
24 <td>${entry.value}</td>
25 </tr>
26 </c:forEach>
27 </table>
28 </body>
29 </html>

分ip统计网站访问次数的更多相关文章

  1. Filter和Listener的应用——分IP统计网站访问次数

    一:分析 统计工作需要在所有资源执行前进行,所以需要放在filter中 这个拦截器仅仅进行统计工作,不进行拦截,所以请求必须继续传递下去 用Map<String,integer>来保存数据 ...

  2. 学习笔记_过滤器应用_1(分ip统计网站的访问次数)

    分ip统计网站的访问次数 ip count 192.168.1.111 2 192.168.1.112 59 统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做 ...

  3. php统计网站访问次数的一个简单方法

    这里主要用到了session保存当前访问者,并将访问次数写入本地文件. <? @session_start(); $counter = intval(file_get_contents(&quo ...

  4. Java web--Filter过滤器分IP统计访问次数

    分IP统计访问次数即网站统计每个IP地址访问本网站的次数. 分析 因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访问次数,所以使用过滤器最为方便. 因为需要分IP统计,所以可以在过滤器中创建 ...

  5. Java web 实现 之 Filter分析ip统计网站的访问次数

    统计工作需要在所有资源之前都执行,那么就可以放到Filter中了. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计的. 用什么东西来装载统计的数据.Map<String,Integer& ...

  6. 【JAVA系列】使用JavaScript实现网站访问次数统计代码

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[JAVA系列]使用JavaScript实现网站 ...

  7. web_02Java ee实现验证码,网站访问次数功能

    Web Web_02版本: 实现功能 1,验证码 2,网站访问次数统计 设计内容 1,servlet 2,jsp 3,js *重点 1,验证码相关: 1,Servlrt类实现验证码的生成 CheckC ...

  8. JSP简单实现统计网页访问次数

    JSP简单实现统计网页访问次数 需求:统计网页的访问次数 核心思想:利用application对象,将访问次数的信息放入application对象中,每次访问就+1.这里利用了application对 ...

  9. 使用javaWeb的二大(Listener、Filter)组件实现分IP统计访问次数

    分析: 统计工作需要在所有资源之前都执行,那么就可以放到Filter中. 我们这个过滤器不打算做拦截操作!因为我们只是用来做统计 用什么东西来装载统计的数据.Map<String,Integer ...

随机推荐

  1. express 直接返回HTML文件

    一般情况下用的是模板引擎,如jade: res.render('detail',{ // 使用render() #http://www.expressjs.com.cn/4x/api.html#res ...

  2. CODEVS-新斯诺克

    原题地址:新斯诺克 题目描述 Description 斯诺克又称英式台球,是一种流行的台球运动.在球桌上,台面四角以及两长边中心位置各有一个球洞,使用的球分别为1 个白球,15 个红球和6 个彩球(黄 ...

  3. Linux终端没有GUI,使用matplotlib绘图

    一.解决警告信息 ... _tkinter.TclError: no display name and no $DISPLAY environment variable 两种解决方法: 1.pytho ...

  4. HDU4641 || 6194多校 (后缀自动机-最少出现K次的字串个数 || 恰好出现K次字符串的个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意: 开始 ...

  5. MySQL自定义排序

    存在表A 按名字倒序排 SELECT  *  FROM  A  ORDER  BY  name  DESC 结果如下: 若需要按照王五.张三.李四的顺序排序,使用自定义排序:FIELD() SELEC ...

  6. dubbo集群容错之LoadBalance

    原文地址:Dubbo 源码分析 - 集群容错之 LoadBalance dubbo 提供了4种负载均衡实现,分别是基于权重随机算法的 RandomLoadBalance.基于最少活跃调用数算法的 Le ...

  7. window.name实现跨域

    在 http://www.cnblogs.com/zhuzhenwei918/p/6759459.html 这篇文章中,我提到了几种跨域的方式,这里主要讲解使用window.name实现跨域. 跨域就 ...

  8. HTML 权重标签的使用

    <H>标签通常使用<H1><H2><H3>这3个标签是有权重加分的,<H4>用于无用途的文字标记等 非重要相关字. <H1>有别 ...

  9. exe4j安装及注册

    1 安装 1 下载 exe4j下载地址:http://www.ej-technologies.com/download/exe4j/files.php, 进入网址,选择需要的版本,点击下载就可以了. ...

  10. sourceTree git 空目录从远程仓库克隆代码出现warning: templates not found

    解决办法: 在安装git时没有默认安装到c盘,而是安装到了d盘.在使用SourceTree进行代码克隆时提示warning: templates not found in D:\software\de ...