hi.. stackoverflow team member.
I am trying to use the HttpSession in java to set some values like userid, so I can use the variable till the session remains present.
I am using spring3.0 for the request mapping. My login check code is given below
@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);
}
and my GlobalController class code is given below
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);
}
}
As the user Login my “/login/GetLoginCheck.action” executes and with this I am setting the attribute empoyeeId: 2 to the session.
But I open the same url in another pc in lan and login with another user named having empoyeeId: 1.
The problem I am having is that when I see the log or open the gridpanel having the employeeId:1 it shows me the data belonging to the employeeId:2.
Also the sessionId get override with the newly logged user session.
Please suggest me some way I can try to implement session in a proper way, so I get the data and session belonging to the intended employee only.
Thanks so much for your replies. I am able to solve my problem with making session as class variable.
below is the code I tried to make the session working in my application in the loginCheck method.
and to retrieve the session attribute done with below code