Jsp使用HttpSessionBindingListener實現在線人數記錄

m4ed 9年前發布 | 2K 次閱讀 Java

onLineUser.java 繼承HttpSessionBindingListener實現在線人數記錄功能

package com.trs;

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

/*HttpSessionBindingListener接口有兩方需要實現的方法: public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent) Session創建的時候Servlet容器將會調用valueBound方法;Session刪除的時候則調用valueUnbound方法./ 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
    { 
        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
        { 
            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(); 
    if(!existUser(e.getName()))
    { 
        users.add(e.getName()); 
        System.out.print(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()); 
}

} </pre>

logout.jsp

<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@ page import="com.trs.onLineUser,java.util.*" %>  
<jsp:useBean id="onlineuser" class="com.trs.onLineUser" scope="application"/> 
<html>
<head>
<title>show</title>
</head>  
<body>
<%  
String name=(String)session.getValue("name");
if(name!=null && name.length()!=0)
{
if(onlineuser.deleteUser(name)) 
  out.println(name+"已經退出系統!"); 
else 
  out.println(name+"沒有登陸到系統!"); 
}
%>
</body>
</html>

online.jsp

<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@page import="com.trs.onLineUser,java.util.*" %>
<html>
</body>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");

if(name!=null && password!=null) { Cookie cookie1=new Cookie("name", name); cookie1.setMaxAge(100000); response.addCookie(cookie1);

Cookie cookie2=new Cookie("password", password); cookie2.setMaxAge(100000); response.addCookie(cookie2); out.println("完成書寫Cookie!"); } else { out.println("書寫失敗!"); } %> </body> </html></pre>
需要說明的是這種方式適合只有單臺服務器的小網站使用,如果網站有多臺web server則不能使用這種方式記錄在線人數。

 本文由用戶 m4ed 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!