推广 热搜: 行业  设备    系统  参数  经纪    教师  机械  中国 

java 在线人数_javaWeb 在线人数统计

   日期:2024-11-09     作者:caijiyuan    caijiyuan   评论:0    移动:http://dgsw198.xhstdz.com/news/524.html
核心提示:这几天在做一个在线人数统计的程序,我费了好大劲在网上查了一些资料,经过调试学到了一些东西,在这写两个简

这几天在做一个在线人数统计的程序,我费了好大劲在网上查了一些资料,经过调试学到了一些东西,在这写两个简单的程序介绍一下

java 在线人数_javaWeb 在线人数统计

1.新建一个工程OnlineCount,包结构如下图所示

%E5%8C%85%E7%BB%93%E6%9E%84_thumb.jpg

demo1:新建一个OnlineCounter.java

代码如下

4f1150b881333f12a311ae9ef34da474.pngpackagecom.dr.demo2.servlet; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpServlet;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpServletRequest;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpSessionEvent;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpSessionListener; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimportorg.apache.log4j.Logger; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngpublicclassOnlineCounterextendsHttpServletimplementsHttpSessionListener 

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gif9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.pngprivatestaticLogger log=Logger.getLogger(OnlineCounter.class);

d18c02628675d0a2c816449d98bda930.pngprivatestaticfinallongserialVersionUID=1L;  

d18c02628675d0a2c816449d98bda930.pngprivatestaticintsessionCounter=0;  

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpubliconlineCounter()9b8a8a44dd1c74ae49c20a7cd451974e.png{  

d18c02628675d0a2c816449d98bda930.png        log.info("OnlineCounter initialized.");  

ecedf933ec37d714bd4c2545da43add2.png    }97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidsessionCreated(HttpSessionEvent se)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        sessionCounter++;  

d18c02628675d0a2c816449d98bda930.png        log.info("session created:"+sessionCounter);  

ecedf933ec37d714bd4c2545da43add2.png    }97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidsessionDestroyed(HttpSessionEvent se)9b8a8a44dd1c74ae49c20a7cd451974e.png

d18c02628675d0a2c816449d98bda930.png        sessionCounter--;  

d18c02628675d0a2c816449d98bda930.png        log.info("session destroied");  

ecedf933ec37d714bd4c2545da43add2.png    }97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicstaticintgetonlineSession()9b8a8a44dd1c74ae49c20a7cd451974e.png{  

d18c02628675d0a2c816449d98bda930.pngreturnsessionCounter;  

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

8f1ba5b45633e9678d1db480c16cae3f.png}4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

在web.xml中写入以下语句

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png    com.dr.demo2.servlet.OnlineCounter   

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png14f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

新建一个online.jsp,在body中写入以下语句:

在线: 人

这样就可以启动服务器运行。

1.初始启动服务器运行时:控制台上会下面的日志信息

2011-01-18 11:11:47,968 [main] [com.dr.demo2.servlet.OnlineCounter] [INFO] - onlineCounter initialized.

2.在浏览器地址栏中中访问online.jsp:控制台上打出,

2011-01-18 11:37:16,031 [http-8080-1] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session created:1

3.大约一分钟过后,控制台上会打出

2011-01-18 11:39:10,265 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session destroied

这跟在web.xml中的时间有关系

在调试时你会发现,当一个电脑上装有不同的浏览器时,用另一个浏览器再次访问时,会打出:

2011-01-18 11:38:16,031 [http-8080-1] [com.dr.demo2.servlet.OnlineCounter] [INFO] - session created:2

为了避免上述失误产生,在程序中加入IP过滤

demo2:新建一个SessionCounter.java

代码如下

4f1150b881333f12a311ae9ef34da474.pngpackagecom.dr.demo2.servlet; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimportjava.io.IOException;

4f1150b881333f12a311ae9ef34da474.pngimportjava.sql.Timestamp;

4f1150b881333f12a311ae9ef34da474.pngimportjava.util.ArrayList; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.ServletException;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.ServletRequestEvent;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.ServletRequestListener;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpServletRequest;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpServletResponse;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpSession;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpSessionEvent;

4f1150b881333f12a311ae9ef34da474.pngimportjavax.servlet.http.HttpSessionListener; 

4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.pngimportorg.apache.log4j.Logger; 

4f1150b881333f12a311ae9ef34da474.png

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gifpublicclassSessionCounterimplementsHttpSessionListener,ServletRequestListener9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.pngprivatestaticLogger log=Logger.getLogger(SessionCounter.class);

d18c02628675d0a2c816449d98bda930.pngprivatestaticfinalString CONTENT_TYPE="text/html; charset=GBK";

d18c02628675d0a2c816449d98bda930.pngprivatestaticintactiveSessions=0;//当前活动的人数d18c02628675d0a2c816449d98bda930.pngprivateHttpServletRequest request;

d18c02628675d0a2c816449d98bda930.pngprivatestaticArrayList list=newArrayList();//用来存放不同ip的地址d18c02628675d0a2c816449d98bda930.png97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidinit()throwsServletException9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter init!"); 

d18c02628675d0a2c816449d98bda930.png

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter doGet!");

d18c02628675d0a2c816449d98bda930.png        response.setContentType(CONTENT_TYPE);

d18c02628675d0a2c816449d98bda930.png        HttpSession session=request.getSession();

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoiddestroy()9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter destroy!");

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidrequestDestroyed(ServletRequestEvent event)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png//To change body of implemented methods use File | Settings | File Templates.d18c02628675d0a2c816449d98bda930.pnglog.info("SessionCounter requestDestroyed!");

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidrequestInitialized(ServletRequestEvent sre)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        request=(HttpServletRequest)sre.getServletRequest();

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter requestInitialized!");

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidsessionCreated(HttpSessionEvent httpSessionEvent)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter sessionCreater!");

d18c02628675d0a2c816449d98bda930.png        String sessionId=httpSessionEvent.getSession().getId();

d18c02628675d0a2c816449d98bda930.png        Timestamp createTime=newTimestamp(System.currentTimeMillis());

d18c02628675d0a2c816449d98bda930.png        String loginIp=request.getRemoteAddr();

d18c02628675d0a2c816449d98bda930.pngbooleanrs=true;

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(list.size()>0)9b8a8a44dd1c74ae49c20a7cd451974e.png{

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.giffor(inti=0;i{

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(loginIp.equals(list.get(i)))9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png                    rs=false;

ecedf933ec37d714bd4c2545da43add2.png                }ecedf933ec37d714bd4c2545da43add2.png            }ecedf933ec37d714bd4c2545da43add2.png        }97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(rs)9b8a8a44dd1c74ae49c20a7cd451974e.png{//如果队列中存在相同的IP 则SESSION不增加d18c02628675d0a2c816449d98bda930.pnglist.add(loginIp);

d18c02628675d0a2c816449d98bda930.png           log.info("ipList队列新增ip:"+loginIp);

d18c02628675d0a2c816449d98bda930.png            activeSessions++;

d18c02628675d0a2c816449d98bda930.png            log.info("新增SESSION,sessionId ="+sessionId+"; createTime ="+createTime

d18c02628675d0a2c816449d98bda930.png+"; loginIp ="+loginIp+"; 当前总SESSION值为"+activeSessions);

ecedf933ec37d714bd4c2545da43add2.png        }ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidsessionDestroyed(HttpSessionEvent httpSessionEvent)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter sessionDestroyed!");

d18c02628675d0a2c816449d98bda930.png        String sessionId=httpSessionEvent.getSession().getId();

d18c02628675d0a2c816449d98bda930.png        Timestamp overTime=newTimestamp(System.currentTimeMillis());

d18c02628675d0a2c816449d98bda930.png        String loginIp=request.getRemoteAddr();

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(activeSessions>0)9b8a8a44dd1c74ae49c20a7cd451974e.png{

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(list.size()>0)9b8a8a44dd1c74ae49c20a7cd451974e.png{

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.giffor(inti=0;i{

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(loginIp.equals(list.get(i)))9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png                        list.remove(i);  

d18c02628675d0a2c816449d98bda930.png                        log.info("ipList队列移除ip:"+loginIp);

ecedf933ec37d714bd4c2545da43add2.png                    }ecedf933ec37d714bd4c2545da43add2.png                }ecedf933ec37d714bd4c2545da43add2.png            }d18c02628675d0a2c816449d98bda930.png            activeSessions--;//在用户销毁的时候,从队列中踢出这个IPd18c02628675d0a2c816449d98bda930.pnglog.info("销毁SESSION,sessionId ="+sessionId+"; overTime ="+overTime

d18c02628675d0a2c816449d98bda930.png+"; loginIp ="+loginIp+"; 当前总SESSION值为"+activeSessions);

ecedf933ec37d714bd4c2545da43add2.png        }ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicstaticintgetActiveSessions()9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter getActiveSessions!");

d18c02628675d0a2c816449d98bda930.pngreturnactiveSessions;

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifpublicvoidsetActiveSessions(inti)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        log.info("SessionCounter setActiveSessions!");

d18c02628675d0a2c816449d98bda930.png        activeSessions=i;

ecedf933ec37d714bd4c2545da43add2.png    }d18c02628675d0a2c816449d98bda930.png

8f1ba5b45633e9678d1db480c16cae3f.png}4f1150b881333f12a311ae9ef34da474.png

4f1150b881333f12a311ae9ef34da474.png

将xml中的代码改为如下代码:

4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.pngcom.dr.demo2.servlet.SessionCounter4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png14f1150b881333f12a311ae9ef34da474.png4f1150b881333f12a311ae9ef34da474.png

online.jsp代码改为如下代码

在线: 人

程序运行结果

1.访问online.jsp时,控制台输出如下

c476354d1b2b45bbb44605d5449b0ff2.png

2.用不同的浏览器访问时,控制台输出如下

761a052bdbd364aa2e21500390629219.png

3.用不同的电脑访问时,控制台输出如下

0f402347edfb214d120f529a53a13f90.png

4.大概一分钟过后,控制台上输出如下

1033f080c6c426f5e5ee69989a63ce14.png

希望各位大虾批评指正

本文地址:http://dgaty.xhstdz.com/news/524.html    物流园资讯网 http://dgaty.xhstdz.com/ , 查看更多
 
标签: 线人 在线
 
更多>同类行业资讯
0相关评论

新闻列表
企业新闻
推荐企业新闻
推荐图文
推荐行业资讯
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号