我也是新手,好象是用JSP自带的内部对象application来解决,具体的你最好再找点资料看!

解决方案 »

  1.   

    是的,用application对象可以实现,<HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <BODY BGCOLOR="#FFFFFF">
    <%
    //统计在线人数
    application.setAttribute("counter",new Integer(0));
    out.println(application.getAttribute("counter"));
    //记录在线人名
    application.setAttribute("onlineName",new String(""));
    out.println(application.getAttribute("onlineName"));
    %>
    </BODY>
    </HTML>
    count.JSP(用来统计总人数并显示到页面):
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE></head>
    <BODY BGCOLOR="#FFFFFF" onUnload="javascript:cancel();">
    <%
    Integer i=(Integer)application.getAttribute("counter");
    String  name=(String)application.getAttribute("onlineName");
    //StringBuffer total=new StringBuffer(name);
    %><font color="blue">
    <%out.println((String)Session.getAttribute("username"));%>您好:
    </font>
    <br><font color="blue">
    当前在线人数:<%=i%>
    </font>
    <br>
    <font color="red">
    在线人总名册:<%out.println(name);%>
    </font></BODY>
    </HTML><HTML>
    <HEAD>
    <TITLE> New Document </TITLE></head>
    <BODY BGCOLOR="#FFFFFF" onUnload="javascript:cancel();">
    <%
    Integer i=(Integer)application.getAttribute("counter");
    String  name=(String)application.getAttribute("onlineName");
    %>
    <br><font color="blue">
    当前在线人数:<%=i%>
    </font>
    <br>
    <font color="red">
    在线人总名册:<%out.println(name);%>
    <font color="blue">
    <%//out.println((String)Session.getAttribute("username"));%>
    </font>
    </font>
    <script language="javascript">
    <!--
    function cancel(){
    <%
    if(Session.getAttribute("username")!=""&&Session.getAttribute("username")!=null){
    //cancelname为注销的人名
    String cancelname;
    cancelname=(String)Session.getAttribute("username");
    //out.println(cancelname);//j为cancelname在总名册中的初始位置
    //k为cancelname的总长度
    //t为cancelname在总名册中的结束位置
    int j,k,t;
    j=name.indexOf(cancelname);
    k=cancelname.length();
    t=j+k;
    //out.println(j);
    //out.println(t);
    ///总名词中去除cancelname
    StringBuffer total=new StringBuffer(name);
    total=total.delete(j-4,t);
    //总人数减1
    i=new Integer(i.intValue()-1);
    application.setAttribute("counter",i);
    name=(String)total.toString();
    application.setAttribute("onlineName",name);
    //out.println("window.alert('"+name+"')");Session.setAttribute("username","");
    }
    %>
    }
    -->
    </script>
    </BODY>
    </HTML>
      

  2.   

    你可以用HttpSessionListener来实现,用它来监听session!
       定义一个int number;
     public void sessionCreated(HttpSessionEvent ev){
         number++;
    ]
    public void sessionDestroyed(HttpSessionEvent ev){
        number--;
    ]
    类似这样,具体的功能你自己实现,即监听session的数量来控制在线人数!