使用servlet统计网站在线人数的方法

  统计再线人数的关键所在应该是查看有多少 活动的Session ,如和获得活动的session呢,使用servlet提供的Listener即监听器就可以解决这样的问题。

    先看看关于Listener的相关API:

  • ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
        
  • ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
        
  • HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
        
  • HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

我选择了使用 HttpSessionAttributeListener。这是个接口提供了3个方法

Method Summary
 void attributeAdded(HttpSessionBindingEvent se)
          Notification that an attribute has been added to a session.
 void attributeRemoved(HttpSessionBindingEvent se)
          Notification that an attribute has been removed from a session.
 void attributeReplaced(HttpSessionBindingEvent se)
          Notification that an attribute has been replaced in a session.
 

   那么我具体实现是,当用户向session中添加属性时。一般是网站用户成功登陆后。attributeAdded (HttpSessionBindingEvent se)将其捕获,并且使用se.getSession()获得其session对象。(因为我做的是显示用户的在线人数,必须是注册用户所以用 HttpSessionAttributeListener中的attributeAdded捕获,而没有用HttpSessionListener在创 建session时捕获。因为可能是没有注册的用户访问网站)

   捕获后,我将session中的属性取出。这个属性表示的是用户的id No.并且保存到Vector中。这样,一旦又用户成功登陆,并把用户信息保存到session中我就可以将其捕获并把id No放到Vector变量中。

   这个监听器的代码如下:

import javax.servlet.*; 
import javax.servlet.http.*;
import java.util.*;

public class SessionListener implements HttpSessionListener,HttpSessionAttributeListener{
 
 private static int activesessions = 0;
 private static Vector idnoC=new Vector(); //用户的idno集合
 
 public void sessionCreated(HttpSessionEvent sess) {
  System.out.println("sessionCreated()/n");
 }

 public void sessionDestroyed(HttpSessionEvent sess) {
  System.out.println("sessionDestroyed()/n");
 }

 /**
  * 往session中增加属性
  */
 public void attributeAdded(HttpSessionBindingEvent arg0) {
  int idno=0;
  if(arg0.getName().equals("idno")){
   idno=(Integer)arg0.getValue();
   addNewVector(idno);
   System.out.println("attributeAdded("+idno+")/n");
  }

 }

 public void attributeRemoved(HttpSessionBindingEvent arg0) {
  int idno=0;
  if(arg0.getName().equals("idno")){
   idno=(Integer)arg0.getValue();
   removeVector(idno);
   System.out.println("attributeRemoved("+idno+")/n"); 
  }
   
 }

 public void attributeReplaced(HttpSessionBindingEvent arg0) {
  int idno=0,orgidno=0;
  if(arg0.getName().equals("idno")){
   orgidno=(Integer)arg0.getValue();
   idno=(Integer)arg0.getSession().getAttribute("idno");
   modifyVector(orgidno,idno);
   System.out.println("attributeReplaced("+orgidno+","+idno+")/n");
  }
 } 
 
 /**
  * 将登陆人员的idno加入到vector中
  * @param idno 登陆人员的idno
  */
 private void addNewVector(int idno){
  if(idnoC.indexOf(new Integer(idno))<0)
   idnoC.add(new Integer(idno));
 }
 
 /**
  * 登陆人员改变时改变vector中的对应idno
  * @param orgidno 原先的idno
  * @param newidno 修改后的idno
  */
 private void modifyVector(int orgidno,int newidno){
  int st=idnoC.indexOf(new Integer(orgidno));
  if(st>0){
   idnoC.removeElement(new Integer(orgidno));   
  }
  addNewVector(newidno);
 }
 
 /**
  * 登陆人员session作废时从vector中移除idno
  * @param idno 员工idno
  */
 private void removeVector(int idno){
  idnoC.removeElement(new Integer(idno));
 }
 
 public static int getactivesessions() { 
  return idnoC.size(); 
 }
 
 public static Vector<Integer> getIdnoCVector(){
  return idnoC;
 }
}

在web.xml中还需要配置监听器

加入这一段

<!-- LISTENER -->
<listener>
 <listener-class>
 XX.XXXX.XXX.SessionListener   <!--类的路径 -->
 </listener-class>
</listener>

那么SessionListener类会在tomcat启动时创建对象。并一直存在,其中声明的Vector相当于全局变量。只要在需要显示现在用户的页面里读取Vector里的内容即可以知道。在先用户的id No.了,其他统计人数就可以直接用Vector里的.size()属性得到

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wp500

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值