这几天在做一个在线人数统计的程序,我费了好大劲在网上查了一些资料,经过调试学到了一些东西,在这写两个简单的程序介绍一下:
1.新建一个工程OnlineCount,包结构如下图所示:
demo1:新建一个OnlineCounter.java
代码如下:
packagecom.dr.demo2.servlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSessionEvent;
importjavax.servlet.http.HttpSessionListener;
importorg.apache.log4j.Logger;
publicclassOnlineCounterextendsHttpServletimplementsHttpSessionListener
{
privatestaticLogger log=Logger.getLogger(OnlineCounter.class);
privatestaticfinallongserialVersionUID=1L;
privatestaticintsessionCounter=0;
publiconlineCounter(){
log.info("OnlineCounter initialized.");
}
publicvoidsessionCreated(HttpSessionEvent se){
sessionCounter++;
log.info("session created:"+sessionCounter);
}
publicvoidsessionDestroyed(HttpSessionEvent se){
sessionCounter--;
log.info("session destroied");
}
publicstaticintgetonlineSession(){
returnsessionCounter;
}
}
在web.xml中写入以下语句:
com.dr.demo2.servlet.OnlineCounter
1
新建一个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
代码如下:
packagecom.dr.demo2.servlet;
importjava.io.IOException;
importjava.sql.Timestamp;
importjava.util.ArrayList;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequestEvent;
importjavax.servlet.ServletRequestListener;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importjavax.servlet.http.HttpSessionEvent;
importjavax.servlet.http.HttpSessionListener;
importorg.apache.log4j.Logger;
publicclassSessionCounterimplementsHttpSessionListener,ServletRequestListener{
privatestaticLogger log=Logger.getLogger(SessionCounter.class);
privatestaticfinalString CONTENT_TYPE="text/html; charset=GBK";
privatestaticintactiveSessions=0;//当前活动的人数privateHttpServletRequest request;
privatestaticArrayList list=newArrayList();//用来存放不同ip的地址
publicvoidinit()throwsServletException{
log.info("SessionCounter init!");
}
publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException{
log.info("SessionCounter doGet!");
response.setContentType(CONTENT_TYPE);
HttpSession session=request.getSession();
}
publicvoiddestroy(){
log.info("SessionCounter destroy!");
}
publicvoidrequestDestroyed(ServletRequestEvent event){
//To change body of implemented methods use File | Settings | File Templates.log.info("SessionCounter requestDestroyed!");
}
publicvoidrequestInitialized(ServletRequestEvent sre){
request=(HttpServletRequest)sre.getServletRequest();
log.info("SessionCounter requestInitialized!");
}
publicvoidsessionCreated(HttpSessionEvent httpSessionEvent){
log.info("SessionCounter sessionCreater!");
String sessionId=httpSessionEvent.getSession().getId();
Timestamp createTime=newTimestamp(System.currentTimeMillis());
String loginIp=request.getRemoteAddr();
booleanrs=true;
if(list.size()>0){
for(inti=0;i{
if(loginIp.equals(list.get(i))){
rs=false;
} } }
if(rs){//如果队列中存在相同的IP 则SESSION不增加list.add(loginIp);
log.info("ipList队列新增ip:"+loginIp);
activeSessions++;
log.info("新增SESSION,sessionId ="+sessionId+"; createTime ="+createTime
+"; loginIp ="+loginIp+"; 当前总SESSION值为"+activeSessions);
} }
publicvoidsessionDestroyed(HttpSessionEvent httpSessionEvent){
log.info("SessionCounter sessionDestroyed!");
String sessionId=httpSessionEvent.getSession().getId();
Timestamp overTime=newTimestamp(System.currentTimeMillis());
String loginIp=request.getRemoteAddr();
if(activeSessions>0){
if(list.size()>0){
for(inti=0;i{
if(loginIp.equals(list.get(i))){
list.remove(i);
log.info("ipList队列移除ip:"+loginIp);
} } } activeSessions--;//在用户销毁的时候,从队列中踢出这个IPlog.info("销毁SESSION,sessionId ="+sessionId+"; overTime ="+overTime
+"; loginIp ="+loginIp+"; 当前总SESSION值为"+activeSessions);
} }
publicstaticintgetActiveSessions(){
log.info("SessionCounter getActiveSessions!");
returnactiveSessions;
}
publicvoidsetActiveSessions(inti){
log.info("SessionCounter setActiveSessions!");
activeSessions=i;
}
}
将xml中的代码改为如下代码:
com.dr.demo2.servlet.SessionCounter1
online.jsp代码改为如下代码:
在线: 人
程序运行结果:
1.访问online.jsp时,控制台输出如下:
2.用不同的浏览器访问时,控制台输出如下:
3.用不同的电脑访问时,控制台输出如下:
4.大概一分钟过后,控制台上输出如下:
希望各位大虾批评指正!