HttpSessionBindingListener實現與應用

來源:互聯網
上載者:User

HttpSessionBindingListener介面有兩方需要實現的方法:
public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)
public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)

Session建立的時候Servlet容器將會調用valueBound方法;Session刪除的時候則調用valueUnbound方法.

具體應用可以跟蹤每個在登入使用者,記錄相關的資訊,以及在其逾時的時候及時的刪除相關的資訊.

執行個體代碼如下:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
....
....
public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent){
//檢查是否有效
if(!Valid()) return;
//檢查資訊是否已經存在.不存在則添加.

if(Info.online.containsKey(m_userId))
{
//記錄日誌資訊
log.info("initial user context....");
UserOnlineStruct UOS = (UserOnlineStruct)Info.online.get(m_userId);
UOS.loginIp = uos.loginIp;
UOS.loginTime = uos.loginTime;
UOS.sessionId = uos.sessionId;
}
else{
Info.online.put(m_userId, uos);
}
}

public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent){
//檢查是否有效
if(!Valid()) return;
//刪除使用者相關資訊
//記錄日誌資訊
log.info("destory user context....");
Info.online.remove(m_userId);
}

--------------------------------
如果你沒有使用log4j的話,你可以把log.info()改為System.out.println()會得到同樣的結果.
如果你想配置log4j,請點擊日誌記錄的使用方法

如果你還有什麼問題,請與我聯絡:webmaster@bcxy.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.