在 GWT 应用程序中使用会话
我有一个 GWT 应用程序,其后端有 RPC 服务。我目前正在尝试实现用户支持,唯一仍然存在的问题是我应该如何存储会话数据。
我使用存储会话 ID
getThreadLocalRequest().getSession().setAttribute("sid", "randomSIDgoeshere");
因此,第一个问题更多的是 Java servlet,而不是 GWT。这段代码是否保证下次我进行这样的调用时:
getThreadLocalRequest().getSession().getAttribute("sid");
它要么为空(如果尚未访问设置 SID 属性的代码段的用户调用它),要么完全相同我已经为该用户保存了 SID。换句话说,这两段代码是用户特定的吗? (用户是指一台计算机上的单个浏览器)
第二个问题是关于存储 SID 和一些额外数据(例如用户 ID)之间的映射。如果我有这样的代码:
public class MyGwtServiceImpl extends RemoteServiceServlet implements MyGwtService {
// SID to User ID mappings
private final Map<String, String> sessions =
new HashMap<String, String>();
...
}
是否可以保证 sessions
对于所有请求始终是同一对象,并且除非整个应用程序终止,否则其数据将保持“活动”状态? (例如 Tomcat 已停止)这是正常方法还是我应该将所有这些映射保留在我的数据库上?
I have a GWT application that has RPC service on its back end. I'm currently trying to implement users support and the only question that still remains is the way I should store session data.
I'm storing session id using
getThreadLocalRequest().getSession().setAttribute("sid", "randomSIDgoeshere");
So, the first question is more to Java servlets than to GWT. Does this code guarantee that next time I make a call like this:
getThreadLocalRequest().getSession().getAttribute("sid");
It would either be null (in case it gets called for the user that hasn't yet visited the piece of code where SID attribute is set) or it would exactly the same SID I've already save for that user. In other words, are these 2 pieces of code user-specific? (by user I mean single browser on a single computer)
The second question is about storing the mappings between SIDs and some extra data like user id. In case I have a code like this:
public class MyGwtServiceImpl extends RemoteServiceServlet implements MyGwtService {
// SID to User ID mappings
private final Map<String, String> sessions =
new HashMap<String, String>();
...
}
Is it guaranteed that sessions
is always the same object for all requests and its data will remain "alive" unless the whole application is terminated? (Tomcat is stopped for instance) Is it normal approach or I should persist all these mappings on my DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

第一:
是的,它确实保证。下次您在下一个请求中调用
getThreadLocalRequest().getSession().getAttribute("sid");
时,属性 sid 将保留在那里。但请记住,这是本地请求区域,因此只有来自同一用户(浏览器+ IP)的请求才能共享信息。这意味着:事实:
情况 1
情况 2
情况 3
因此,是的,会话的内容是特定于用户的。一个会话中存在的内容并不意味着其他会话中也会存在。
第二:
不,不能保证。尽管大多数时候都会调用相同的servlet实例,但不能保证它总是存在。如果您想在servlet 中保留属性,则必须将这些属性声明为静态,这样,该静态属性将不再是特定于用户的。或者你可以存储在 ServeletContext 中
我这样说是因为如果长时间不需要该servlet,不同的实现(例如 Glassfish)可以终止实例(据我所知,我不确定这一点(Glassfish终止实例))。但没有文档表明它确实保证是同一个实例,因此您不能声明非静态属性并在不同实例之间共享。
First:
Yes, it does guarantee. The next time you call
getThreadLocalRequest().getSession().getAttribute("sid");
on the next request, the attribute sid will stay there. But remember, that's the local request area, thus only requests from the same user (browser + ip) shall share the information. That means:Fact:
Case 1
Case 2
Case 3
So yes, the session's content is user-specific. What exists in one session does not imply that will exist in other session.
Second:
No, it is not guaranteed. Although most of the times the same servelet isntance will be called, it is not guaranteed that will always exist. If you want to persist with attributes in your servelet, you must declare those attributes as Static, and by so, THAT static attribute won't be user-specific. Or you can store in the ServeletContext
I say this because different implementations (like Glassfish) can terminate instances if the servelet isn't being required for a long period of time (as far as I remember, I'm not sure of this (Glassfish terminating the instance)). But there is no documentation saying that it does guarantee that will be the same instance, so you cannot declare non-static attributes and share between diferent instances.