简体   繁体   English

在已部署的系统中使用session.setAttribute

[英]Using session.setAttribute in a deployed system

I have a system that is deployed with the link http://192.168.2.6:8484/DTR and upon logging in, it stores the user's info via session.setAttribute("user", user); 我有一个使用链接http://192.168.2.6:8484/DTR部署的系统,登录后,它通过session.setAttribute("user", user);存储用户信息session.setAttribute("user", user); However, when another user logs in, it overwrites the info of the first user as it again calls session.setAttribute("user", user); 但是,当另一个用户登录时,它将再次调用session.setAttribute("user", user);覆盖第一个用户的信息session.setAttribute("user", user); . So how can I really save the user's info so that more than two people can access the system at the same time? 那么,如何真正保存用户的信息,以便两个以上的人可以同时访问系统?

This is what is currently happening: 这是当前正在发生的事情:

I have two websites that are open. 我有两个开放的网站。

  1. I login in the first website (username: user1). 我登录第first网站(用户名:user1)。 Shows Hello, user1 显示Hello, user1
  2. I login in the second website (username: user2). 我登录second网站(用户名:user2)。 Shows Hello, user2 显示Hello, user2
  3. I refresh the first website. 我刷新了first网站。 It will now show Hello, user2 现在它将显示用户2 Hello, user2

So how can I enable multiple users to access the website? 那么如何使多个用户访问该网站?

You first get user attribute and set it if it's not there and session is new 您首先要获取用户属性,如果不存在并且会话是新的,请对其进行设置

user = session.getAttribute("user");

if (user == null&& session.isNew())
   session.setAttribute("user", user);

Also please check if you are getting different session for different users session.getId() ... if not it might be the problem JSESSIONID cookie. 另外,请检查您是否为不同的用户获得不同的会话session.getId()...如果不是,则可能是JSESSIONID cookie问题。 The servletcontainer set a Cookie in the Set-Cookie header of the HTTP response with JSESSIONID as cookie name and the unique session ID as cookie value. servlet容器在HTTP响应的Set-Cookie标头中设置一个Cookie,其中JSESSIONID作为cookie名称,而唯一会话ID作为cookie值。

As discussed in the comments, the reason is both the users are logged in from the same browser and same system. 如评论中所述,原因是两个用户都从相同的浏览器和相同的系统登录。 So the JSessionId is same and hence the attributes are overriden. 因此,JSessionId相同,因此属性被覆盖。

Solution :Try with a different browser 解决方案 :尝试使用其他浏览器

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

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