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

纵然是瞬间 提交于 2019-12-06 08:16:07

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