package com;import javax.servlet.http.*; 
//import javax.servlet.*; 
import java.util.*; public class onLineUser implements HttpSessionBindingListener { 
     public onLineUser(){
   }    private Vector users=new Vector();
   public int getCount(){
       users.trimToSize();
       return users.capacity();
   }
   public boolean existUser(String userName){
       users.trimToSize();
       boolean existUser=false;
       for (int i=0;i<users.capacity();i++ )
       {
           if (userName.equals((String)users.get(i)))
           {
               existUser=true;
               break;
           }
       }
       return existUser;
   }   public boolean deleteUser(String userName) {
       users.trimToSize();
       if(existUser(userName)){
           int currUserIndex=-1;
            for(int i=0; i<users.capacity();i++){
               if(userName.equals((String)users.get(i))){
                   currUserIndex=i;
                   break;
               }
           }
           if (currUserIndex!=-1){
               users.remove(currUserIndex);
               users.trimToSize();
               return true;
           }
       }
       return false;
   }   public Vector getOnLineUser()
   {
       return users;
   }
 public void valueBound(HttpSessionBindingEvent e) 
 { 
   users.trimToSize();
    HttpSession session = e.getSession();
session.getAttribute("ID");
   if(!existUser(e.getName())){
       users.add(e.getName());
       System.out.print(session.getAttribute("ID")+e.getName()+"\t   登入到系统\t"+(new Date()));
       System.out.println("      在线用户数为:"+getCount());
   }else
       System.out.println(e.getName()+"已经存在");
 }      public void valueUnbound(HttpSessionBindingEvent e) { 
       users.trimToSize();
       String userName=e.getName();
       deleteUser(userName);
       System.out.print(userName+"\t   退出系统\t"+(new Date()));
       System.out.println("      在线用户数为:"+getCount());
     } 

解决方案 »

  1.   

    我是新手,用的是filterServlet,在验证用户登陆的时候,记录该用户信息,如:
    //记录登录用户IP地址
    System.out.println(request.getRemoteAddr());
    //记录登录时间,session一下
    System.out.println(new java.uitl.Date());退出系统时,在注销session前记录退出时间:
    System.out.println(new java.uitl.Date());
    //顺便算一下停留时间,最后session.invalidate()
      

  2.   

    to:jfy3d(剑事) 
    我想用户登录时就将IP和登录时间写入数据库,关闭网站时把退出时间写入数据库,你这个com好象不行吧?
      

  3.   

    public void valueBound(HttpSessionBindingEvent e) 
     { 
       users.trimToSize();
        HttpSession session = e.getSession();
    session.getAttribute("ID");
       if(!existUser(e.getName())){
           users.add(e.getName());
           System.out.print(session.getAttribute("ID")+e.getName()+"\t   登入到系统\t"+(new Date()));
           System.out.println("      在线用户数为:"+getCount());
       }else
           System.out.println(e.getName()+"已经存在");
     }      public void valueUnbound(HttpSessionBindingEvent e) { 
           users.trimToSize();
           String userName=e.getName();
           deleteUser(userName);
           System.out.print(userName+"\t   退出系统\t"+(new Date()));
           System.out.println("      在线用户数为:"+getCount());
         } valueBound
    valueUnbound
      

  4.   

    应该是HttpSessionListner接口啊
    onSessionCrested() 
    onSessionDestroyed()
      

  5.   

    public void valueUnbound(HttpSessionBindingEvent e) 
    这个方法在jsp怎样调用?应该是HttpSessionListner接口啊
    onSessionCrested() 
    onSessionDestroyed()
    能否详细些
      

  6.   

    <jsp:useBean id="mybeans" scope="page" class="com.onLineUser"/>
    在jsp中怎样调用valueUnbound(HttpSessionBindingEvent e)这个方法
    valueUnbound(null);发生错误.
      

  7.   

    <%@ page contentType="text/html;charset=gb2312" %> 
    <%@ page import="list.onLineUser,java.util.*" %> 
    <jsp:useBean id="onlineuser" class="list.onLineUser" scope="application"/>
    <html> 
    <head>
    <title>搞定JSP在线人数</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta http-equiv="refresh"  content="20";url="listuser.jsp">
    <style type="text/css">
    <!--
    @import url(../css/fontsizesequ.css);
    -->
    </style>
    </head>
    <body bgcolor="#57647F" leftmargin="3" topmargin="2">
    <span class="white14">
    <%   session = request.getSession(false); %>
    <% 
       String username=(String)session.getAttribute("ID");
       if (onlineuser.existUser(username)){
           
       }else{
           session.setMaxInactiveInterval(1200); //file://Sesion有效时长,以秒为单位
           session.setAttribute(username,onlineuser); 
           
       }
       out.println("在线人数:"+onlineuser.getCount()+"<br/>");
       Vector vt=onlineuser.getOnLineUser();
       Enumeration e = vt.elements();
       out.println("在线用户列表");
       
         while(e.hasMoreElements()){
       
           out.println((String)e.nextElement());
          
       }    
    %>
    </span> 
    </body> 
    </html> 
      

  8.   

    to  jfy3d(剑事)能不能顺便说一下怎么用呀?
      

  9.   

    如果按你上面这样写,与用不用接口好象关系不大嘛?只需把IP取出来放在vector中,将vector放在session中,把session过期时间放长一点,在用户进入首页时将IP与vector中存放在IP提出来比较,如果没有就将IP写入数据库。但系统现要把用户离开网站的时间记录下来,也要写入数据库。因此需要象你老人家说的用Lister.可是你上面这一段Jsp好象不能完成哪样的功能?搞定就结帐,再帮忙看看?
      

  10.   

    通过一篇文单我终于明白了,呵呵!接口中的这个监听过程是在session创建或失效时自动触发!
    http://tech.ccidnet.com/pub/article/c322_a180671_p1.html
      

  11.   

    valueBound
    valueUnbound
    看jdk doc就知道了
    两个方法里写上你要的操作就行了
      

  12.   

    import javax.servlet.http.*;
    6 import javax.servlet.*;
    7
    8 public class OnLineCountListener implements HttpSessionListener,
    ServletContextListener,ServletContextAttributeListener
    9 {
    10 private int count;
    11 private ServletContext context = null;
    12
    13 public OnLineCountListener()
    14 {
    15 count=0;
    16 //setContext();
    17 }
    18 //创建一个session时激发
    19 public void sessionCreated(HttpSessionEvent se) 
    20 {
    21 count++;
    22 setContext(se);
    23
    24 }
    25 //当一个session失效时激发
    26 public void sessionDestroyed(HttpSessionEvent se) 
    27 {
    28 count--;
    29 setContext(se);
    30 }
    31 //设置context的属性,它将激发attributeReplaced或attributeAdded方法
    32 public void setContext(HttpSessionEvent se)
    33 {
    34 se.getSession().getServletContext().
    setAttribute("onLine",new Integer(count));
    35 }
    36  //增加一个新的属性时激发
    37 public void attributeAdded(ServletContextAttributeEvent event) {
    38
    39 log("attributeAdded('" + event.getName() + "', '" +
    40     event.getValue() + "')");
    41
    42     }
    43     
    44    //删除一个新的属性时激发
    45     public void attributeRemoved(ServletContextAttributeEvent event) {
    46
    47 log("attributeRemoved('" + event.getName() + "', '" +
    48     event.getValue() + "')");
    49
    50     }
    51
    52 //属性被替代时激发
    53     public void attributeReplaced(ServletContextAttributeEvent event) {
    54
    55 log("attributeReplaced('" + event.getName() + "', '" +
    56     event.getValue() + "')");
    57     }
    58     //context删除时激发
    59      public void contextDestroyed(ServletContextEvent event) {
    60
    61 log("contextDestroyed()");
    62 this.context = null;
    63
    64     }
    65
    66     //context初始化时激发
    67     public void contextInitialized(ServletContextEvent event) {
    68
    69 this.context = event.getServletContext();
    70 log("contextInitialized()");
    71
    72     }
    73     private void log(String message) {
    74
    75     System.out.println("ContextListener: " + message);
    76     }   
    77 }
    <font color=red><%=getServletContext().getAttribute("onLine")%></font><br>
    为何onLine的值一直增加,当一个用户关闭浏览器时,onLine的值也不减1?