2012-09-14 39 views

回答

2

我個人更喜歡不要綁定到Tomcat包,並儘可能多地使用javax.servlet API,原因很明顯。你的任務可以通過創建一個會話監聽器,並保持所有會話的WeakHashMap來實現,像這樣

<!-- your webapp's web.xml --> 
<listener> 
    <listener-class>path.to.SessionListener</listener-class> 
</listener> 

class SessionListener implements HttpSessionListener { 
    private static final Map<String, HttpSession> sessions = Collections.synchronizedMap(new WeakHashMap<String, HttpSession>()); 

    public void sessionCreated(HttpSessionEvent event) { 
     sessions.put(event.getSession().getId(), event.getSession()); 
    } 

    public void sessionDestroyed(HttpSessionEvent event) { 
     sessions.remove(event.getSession().getId()); 
    } 
} 

其餘如添加另一種方法,通過所有會話迭代和無效一樣簡單。

0

使用session.invalidate方法爲特定用戶喜歡。

如果您在session.set屬性中設置用戶,則從訪問器方法獲取該particualr方法。

使用pojo bean來處理它。

在使會話無效之前,請檢查它是否爲真或有效。

+0

不能讓你可以簡單介紹一下...... – TaherT