嗨.. stackoverflow团队成员。在Java中使用会话登录模块时遇到困难
我想在java中使用HttpSession来设置一些值像userid,所以我可以使用变量,直到会话保持存在。
我使用spring3.0进行请求映射。下面
@RequestMapping(value = "/login/GetLoginCheck.action")
public @ResponseBody
Map<String, Object> loginCheck(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<Employee> emplist = null;
try {
GlobalController.session = request.getSession();
if (!GlobalController.session.isNew()) {
this.setUsername(request.getParameter("username"));
this.setPassword(request.getParameter("password"));
emplist = loginservice.getEmployee(this.getUsername(),this.getPassword());
if(emplist.size()>0)
{
for(Employee employee: emplist)
{
this.setEmployeeid(employee.getId());
synchronized (GlobalController.session) {
GlobalController.session.setAttribute("userid", employee.getId());
}
}
}else
{
return getModelMapError("The username or password you entered is incorrect.");
}
}else{
System.out.println("already session created");
System.out.println("SESSION ID ::"+GlobalController.session.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
logingview = this.loginView(this.getUsername(),this.getPassword());
return getMapUser(emplist,logingview);
}
我的登录校验码,并给出以下
public class GlobalController implements HttpSessionListener{
private static int totalActiveSessions = 0;
public static HttpSession session = null;
@Override
public void sessionCreated(HttpSessionEvent event) {
// TODO Auto-generated method stub
synchronized (this) {
totalActiveSessions++;
}
System.out.println("Session Created: " + event.getSession().getId());
System.out.println("Total Sessions: " + totalActiveSessions);
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
// TODO Auto-generated method stub
synchronized (this) {
totalActiveSessions--;
}
System.out.println("Session Destroyed: " + event.getSession().getId());
System.out.println("Total Sessions: " + totalActiveSessions);
}
}
我GlobalController类代码作为用户登录我的“/login/GetLoginCheck.action”执行以及与此我设置会话的属性empoyeeId:2。
但我在局域网另一台PC打开相同的URL,并与名为有empoyeeId其他用户登录:1.
我遇到的问题是,当我看到日志或打开具有的GridPanel employeeId:1它显示属于employeeId:2的数据。
此外,sessionId会使用新登录的用户会话进行覆盖。
请给我一些建议,我可以尝试以适当的方式实现会话,所以我得到的数据和会话只属于预期员工。
感谢您的快速反应,但我已经实现了上面的代码正确也一切正常。但只有sessionId存在问题。我总是变成最后登录的用户sessionId。请建议我一些工作解决方案,我可以尝试它并让我的应用程序工作。 –