Reputation: 3204
I am building a struts 2 application with JPA. A user can login into the application multiple times. I want
Thanks
Upvotes: 3
Views: 6656
Reputation: 2427
I think HttpSessionBindingListener
is what are you looking for.
I won't write down the complete code, just suggest you a way you can do it:
You can add a static field (Map) to your User class (DTO)
where you will store all active sessions of users
. :
e.g private static Map<User, HttpSession> usersSessions= new HashMap<User, HttpSession>();
Then make User class
implemets HttpSessionBindingListener
. This way you can specify valueBound(HttpSessionBindingEvent event)
method in which you can grab actually created session
and put it into your usersSessions
like this :
usersSessions.put(this, event.getSession());
In valueUnbound(HttpSessionBindingEvent event)
method then :
usersSessions.remove(this);
to remove users session
after logout
.
This way you have Map
of all of your active sessions
also with information to which user it belongs to. IMO you can figure out your other problems easily with this.
Upvotes: 6