Java – can I expire all user sessions?
•
Java
Can I expire all user sessions?
I'm using tomcat, servlet / JSP
In the session, I have a property userid
On this basis, we define that this session belongs to a specific user
I need to invalidate all sessions that contain a specific userid
Solution
Use this code to manage your active session and get the session by its ID, and then you can call this session and invalidate it if needed:
public class SessionListener implements HttpSessionListener { private static Map<String,HttpSession> map = new HashMap<String,HttpSession>(); public void sessionCreated(HttpSessionEvent event) { String id = event.getSession().getId(); logger.debug("session created : " + id); // STORE THE SESSOIN FOR EXAMPLE IN DATABASE map.put(id,event.getSession()); } public static HttpSession getHttpSession(String sessionID) { return sessionObjectMap.get(sessionID); } public void sessionDestroyed(HttpSessionEvent event) { // get the destroying session... } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码