我使用 Glassfish 服务器 3.1.1,并且我成功地为用户组配置了我的领域。
虽然只有 1 个用户登录,但一切正常,但是当我从不同的浏览器(甚至不同的计算机)使用另一个帐户登录时,第一个 session 被清除,最后登录的用户数据显示在之前的每一次 session 。
我正在使用 Netbeans,让它从 postgreSQL 数据库生成实体类,然后从实体生成 JSF 页面。 我读过有关 HttpSessions 的内容,但我并不清楚所有内容。我应该继续这样,还是解决方案是在不同的方向?如果这是正确的方法,有人可以给我发送示例源吗?
这是我的代码:
登录.xhtml:
<h:inputText id="email" size="25" value="#{login.email}" maxlength="30"/>
<h:inputSecret id="password" size="25" value="#{login.password}" maxlength="100"/>
<h:commandLink value="Bejelentkezés" action="#{login.loginAction}"/>
这就是我的登录类的样子:
@ManagedBean(name="login")
@RequestScoped
public class LoginBean
{
private String email;
private String password;
public String loginAction()
{
HttpServletRequest req=(HttpServletRequest)FacesContext.getCurrentInstance()
.getExternalContext().getRequest();
try
{
System.out.println("login with: " + email + ", " + password + ".");
req.login(email, password);
}
catch(ServletException e) ....
最佳答案
到目前为止发布的代码看起来不错。
but as I log in with another account from a different browser (or even a different computer), the first session is cleared and the last logged in users data is shown in every previous sessions.
此问题是由目前发布的代码以外的其他地方引起的。根本不应该清除 session 。这可能是对正在发生的事情的误解。也许你不理解“ session ”这个概念。这些症状表明您显然是在某些 static
变量或 @ApplicationScoped
托管 bean 中获取了登录用户。确保您没有这样做。
关于“ session ”是如何工作的,请阅读:How do servlets work? Instantiation, sessions, shared variables and multithreading .
关于如何选择合适的托管bean范围,请阅读:How to choose the right bean scope?
关于Java Web 应用程序多用户 session 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7604810/