您可以通過執行HttpSessionListener
來保持對用戶會話的引用。 This example顯示如何實現會話計數器,但您也可以通過將其存儲在上下文作用域集合中來保留對各個會話的引用。然後,您可以從管理頁面訪問會話,檢查其屬性並使其中的一些無效。 This post也可能有用的信息。
編輯:下面是一個簡單的實現(未測試):
public class MySessionListener implements HttpSessionListener {
static public Map<String, HttpSession> getSessionMap(ServletContext appContext) {
Map<String, HttpSession> sessionMap = (Map<String, HttpSession>) appContext.getAttribute("globalSessionMap");
if (sessionMap == null) {
sessionMap = new ConcurrentHashMap<String, HttpSession>();
appContext.setAttribute("globalSessionMap", sessionMap);
}
return sessionMap;
}
@Override
public void sessionCreated(HttpSessionEvent event) {
Map<String, HttpSession> sessionMap = getSessionMap(event.getSession().getServletContext());
sessionMap.put(event.getSession().getId(), event.getSession());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
Map<String, HttpSession> sessionMap = getSessionMap(event.getSession().getServletContext());
sessionMap.remove(event.getSession().getId());
}
}
然後,您可以從任何servlet訪問會話圖:
Collection<HttpSession> sessions = MySessionListener.getSessionMap(getServletContext()).values();
如果用戶是'你怎麼檢查與alpha權限混合? –
@DigvijayYadav以防萬一我的alpha測試人員變得瘋狂...... –
等等,你可能會錯過理解我的問題。我想使他的會話無效。這就是我想要做的,也許是一個管理頁面。 –