Java Web 应用程序多用户会话处理
我使用 Glassfish 服务器 3.1.1,并且我成功地为用户组配置了我的领域。
虽然只有 1 个用户登录,但一切正常,但当我从不同的浏览器(甚至不同的计算机)使用另一个帐户登录时,第一个会话将被清除,最后登录的用户数据将显示在之前的每个会话中。
我使用 Netbeans,让它从 postgreSQL 数据库生成实体类,然后从实体生成 JSF 页面。 我读过有关 HttpSessions 的内容,但我并不清楚所有内容。我应该继续这样,还是解决方案是在不同的方向?如果这是正确的方法,有人可以给我发送示例源吗?
这是我的代码:
Login.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) ....
I use Glassfish server 3.1.1, and I successfully configured my realm for the usergroups.
While only 1 user is logged in, everything works fine, 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.
I'm using Netbeans, and I let it generate my entity classes from a postgreSQL database, then the JSF pages from the entities.
I read about HttpSessions, but there wasn't everything clear to me. Shall I continue this way, or the solution is in a different direction? If this is the right way, could anyone send me a sample source?
Here is my code:
Login.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}"/>
This is how my login class looks like:
@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) ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止发布的代码看起来不错。
除了迄今为止发布的代码之外的其他地方。根本不应该清除会话。这很可能是对所发生事件的误解。也许你不理解“会话”这个概念。这些症状表明您显然是在某些
static
变量或@ApplicationScoped
托管 bean 中获取了登录用户。确保你没有这样做。至于“会话”是如何工作的,请阅读以下内容: Servlet 如何工作?实例化、会话、共享变量和多线程。
至于如何选择合适的托管bean范围,请阅读:如何选择正确的 bean 范围?
The code posted so far looks fine.
This problem is caused elsewhere than in the code posted so far. The session should not be cleared at all. This is likely a misinterpretation of the happening. Perhaps you don't understand the concept "session". The symptoms indicate that you're apparently getting hold of the logged-in user in some
static
variable or an@ApplicationScoped
managed bean. Make sure that you aren't doing that.As to how the "session" works, please read this: How do servlets work? Instantiation, sessions, shared variables and multithreading.
As to how to choose the proper managed bean scope, please read this: How to choose the right bean scope?