Java在WEB中如何统计在线人数

  原理:   建立一个Session级别的Listener,然后在Session建立的时候往Application的一个向量变量List中写入用户信息,当Session释放时从变量List中删除该用户信息,就可以在Application的列表中得到在线用户列表了。

  
  1. import javax.servlet.http.*; 
  2. import javax.servlet.*; 
  3. import java.util.*; 
  4. public class onLineUser implements HttpSessionBindingListener { 
  5.         private Vector users=new Vector();    
  6.                
  7.         public   int  getCount(){ 
  8.                     users.trimToSize(); 
  9.                     return  users.capacity(); 
  10.         } 
  11.         
  12.           public  boolean existUser(String userName){ 
  13.                     users.trimToSize(); 
  14.                      boolean existUser= false
  15.                     for (int i=0;i 
  16.                        { 
  17.                                 if (userName.equals((String)users.get(i))) 
  18.                                    { 
  19.                                             existUser=true
  20.                                             break
  21.                                        } 
  22.                            } 
  23.                         return existUser; 
  24.             }     
  25.             public  boolean deleteUser(String userName) { 
  26.                     users.trimToSize(); 
  27.                     if (existUser(userName)){ 
  28.                                 int currUserIndex=-1; 
  29.                                 for ( int  i=0;i 
  30.                                         if(userName.equals((String)users.get(i))){ 
  31.                                                 currUserIndex=i; 
  32.                                                 break
  33.                                             } 
  34.                       } 
  35.                     if  (currUserIndex!=-1){ 
  36.                                 users.remove(currUserIndex); 
  37.                                 users.trimToSize(); 
  38.                     return true
  39.                     } 
  40.                 } 
  41.                 return   false
  42.             } 
  43.  
  44.             public Vector getOnLineUser() 
  45.             { 
  46.                         return users; 
  47.                 } 
  48.     
  49.                 public void valueBound(HttpSessionBindingEvent e) { 
  50.                             users.trimToSize(); 
  51.                             if(!existUser(e.getName())){ 
  52.                                             users.add(e.getName()); 
  53.                                             System.out.print(e.getName()+"/t 登入到系统/t"+(new Date())); 
  54.                                             System. out .println( " 在线用户数为:" +getCount()); 
  55.                             }else 
  56.                                             System. out .println(e.getName()+ "已经存在" ); 
  57.                             } 
  58.  
  59.                 public void valueUnbound(HttpSessionBindingEvent e) { 
  60.                                     users.trimToSize(); 
  61.                                         String userName=e.getName(); 
  62.                                     deleteUser(userName); 
  63.                                     System.out.print(userName+"/t 退出系统/t"+(new Date())); 
  64.                                     System. out .println( " 在线用户数为:" +getCount()); 
  65.                     } 

 

 

  1. JSP程序
  2. jsp: 
  3. <%@ page contentType="text/html;charset=gb2312" %> 
  4. <%@ page import= "java.util.*"  %> 
  5. "onlineuser" class="temp.jb.onLineUser" scope="application"/> 
  6.  
  7.  
  8.  
  9. "postMessage()"
  10.  
  11.  

    登陆成功,欢迎访问 

     

     
  12.  
  13. <% session = request.getSession(false); %> 
  14. <% 
  15. String username=request.getParameter("username"); 
  16. if  (onlineuser.existUser(username)){ 
  17. out.println("用户 "+username+已经登陆!"); 
  18. } else
  19. session.setMaxInactiveInterval(50); //Sesion有效时长,以秒为单位 
  20. session.setAttribute(username,onlineuser); 
  21. out.println("欢迎新用户: "+username+登陆到系统!"); 
  22. out.println(
    当前在线用户人数: "
    +onlineuser.getCount()+ 
    "
    ); 
  23. String ip = request.getRemoteAddr(); 
  24. out.println(
    IP: "
    +ip+ 
    "
    ); 
  25. Vector vt=onlineuser.getOnLineUser(); 
  26. Enumeration e = vt.elements(); 
  27. out .println( "在线用户列表" ); 
  28. out.println("); 
  29. out .println( "  用户名  " ); 
  30. while(e.hasMoreElements()){ 
  31. out .println( "  " ); 
  32. out.println((String)e.nextElement()+
    "
    ); 
  33. out .println( "  " ); 
  34. out .println( " " ); 
  35. %> 
  36.  
  37.  

     
  38. "javascript:window.close()">关闭窗口 ] 
  39. <% 
  40. out.println(

     +username+"'>退出系统  

    "
    ); 
  41. %> 
  42.  
  43.  
  44.  

你可能感兴趣的:(Java在WEB中如何统计在线人数)