Is there any method to obtain in a servlet all valid session keys values on jetty?

纵饮孤独 提交于 2019-12-22 22:21:52

问题


I have a jetty container with two different servlets, lets call then A and B. In a special occasion a qr code code appear in servlet A (the user is already logged in and is using his desktop) and the user by using his mobile device read this qr code and is redirected the servlet B on his mobile device. The problem here is that i cant keep his session.

The QR code brings the user session key however i have no way to verify if this session is valid. I would like to know if there is any special method to request the valid session keys on jetty, since both servlet are in the same server.


回答1:


Well the best solution i found was to establish a HttpSessionListener :) for that we have 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 on /WEB-INF/web.xml

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

Now we can call at anywhere inside the package the HttpSessionCollector. e.g. to obtain all valid sessions we have just to:

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


来源:https://stackoverflow.com/questions/10770535/is-there-any-method-to-obtain-in-a-servlet-all-valid-session-keys-values-on-jett

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!