Is there a way to get all valid session key values ​​on a berth in a servlet?

I have a berth container with two different servlets, then let's call A and B. In a special case, the qr code code appears in servlet A (the user is already logged in and uses his desktop), and the user has read this code using his mobile device qr and redirected servlet B to his mobile device. The problem here is that I cannot continue my session.

The QR code brings the user session key, however I have no way to check if this session is valid. I would like to know if there is any special method for requesting the correct session keys on the marina, since both servlets are on the same server.

+2
source share
1 answer

Well, the best solution I found is to set the HttpSessionListener :) for this we need to override some methods:

public class HttpSessionCollector implements HttpSessionListener { private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>(); @Override public void sessionCreated(HttpSessionEvent event) { HttpSession session = event.getSession(); sessions.put(session.getId(), session); } @Override public void sessionDestroyed(HttpSessionEvent event) { sessions.remove(event.getSession().getId()); } public static HttpSession find(String sessionId) { return sessions.get(sessionId); } public static Map<String, HttpSession> getSessions() { return sessions; } 

}

and then set the listener to / WEB -INF / web.xml

 <web-app> <listener> <listener-class>[yourpack].HttpSessionCollector</listener-class> </listener> ... </web-app> 

Now we can call anywhere in the HttpSessionCollector package. for example, to get all valid sessions, we only have:

 private Map<String, HttpSession> sessions; sessions=HttpSessionCollector.getSessions(); 
+7
source

Source: https://habr.com/ru/post/972959/


All Articles