Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

Access all User Session in Struts 2

I am building a struts 2 application with JPA. A user can login into the application multiple times. I want

  1. user to be able to view all his session in a grid and probably highlight the current session and optionally the user can select a session and terminate it.
  2. An administrator should also be able to see all the logged in users and can also view all the sessions of each logged in user and also can optionally terminate any of the sessions.

Thanks

Upvotes: 3

Views: 6656

Answers (1)

Fallup
Fallup

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

Related Questions