如何检查现有 session 是否无效? 在下面的 loginBean 代码中,我检查了用户是否已经登录的每个登录。 如果已经由其他系统登录,请停用该 session 并创建新的。如果该 session 因超时而无效,则下一次 userSessionMap 包含 usedname 和错误以使其无效。那么如何检查它是否无效呢?
Map appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
Map<String, HttpSession> userSessionMap = (Map<String, HttpSession>) appMap.get("USERS");
if (userSessionMap.containsKey(getUsername())) {
log.info(getUsername() + " already loggedin. Closing existing sesssion");
HttpSession sess = userSessionMap.get(getUsername());
sess.invalidate();
}
最佳答案
尝试将 false
作为参数传递给 getSession(boolean) .这将返回一个 session (如果它存在),否则它将返回 null
。
HttpSession session = request.getSession(false);
if (session == null || !request.isRequestedSessionIdValid()) {
//comes here when session is invalid.
}
Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session. If create is false and the request has no valid HttpSession, this method returns null.
关于java - 如何检查现有 session 是否无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183847/