简体   繁体   English

是否有任何方法可以在servlet中获取码头上所有有效的会话密钥值?

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

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. 我有一个带有两个不同Servlet的码头容器,让我们依次调用A和B。在特殊情况下,一个QR代码出现在Servlet A中(用户已经登录并且正在使用其桌面),并且用户使用移动设备读取此qr代码,并将其重定向到其移动设备上的servletB。 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. QR码带有用户会话密钥,但是我无法验证此会话是否有效。 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. 我想知道是否有任何特殊方法来请求码头上的有效会话密钥,因为两个Servlet都在同一服务器上。

Well the best solution i found was to establish a HttpSessionListener :) for that we have to override some methods: 好吧,我发现的最佳解决方案是建立一个HttpSessionListener :),为此我们必须重写一些方法:

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-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. 现在,我们可以在包内的任何位置调用HttpSessionCollector。 eg to obtain all valid sessions we have just to: 例如,为了获得所有有效的会话,我们只需要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM