Track the user session list
How do we have a track of users who have logged in? I want to have a table of record with usernames and the status(logged in or logged out).
Whenever the user logs in, the status shud indicate he is logged in and when he logs out the status shud change accordingly. Does anybody know how to do this???
Re: Track the user session list
I take it you are handling the login/logout your self, as in looking up the user credentials from somewhere and matching them with the user input.
Just have it so that whenever a login is performed you add the user to a list of logged in users, maybe a static list somewhere like a SessionStatistics class or whatever.
Whenever a user logs out you remove the user from the list. Better make the list a map I guess.
However if you also which to make sure that whenever a session is destroyed by the application server you can have your session manager implement HttpSessionListener.
Here is a quick example.
Code :
public class SessionManager implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent sessionEvent) {
// This is called whenever a new session is created, no matter if the user is logged in or not
}
@Override
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
// This is called whenever a session is destroyed
// In here you can have some code that gets the session off the sessionEvent object and then checks to see if this session represents a user who was logged in. Since the session is now about to be destroyed you can then safely remove that user from your map/list of logged in users.
}
}
Of course you also need to add this listener in your web.xml file.
Code :
<listener>
<listener-class>my.package.name.SessionManager</listener-class>
</listener>
Hope that makes any sense. If you need more help in looking at the specific code for adding/removing the user from the map/list, let us know and I shall see what I can do.
// Json
Re: Track the user session list
Code :
public class SessionManager implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent sessionEvent) {
// This is called whenever a new session is created, no matter if the user is logged in or not
}
@Override
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
// This is called whenever a session is destroyed
// In here you can have some code that gets the session off the sessionEvent object and then checks to see if this session represents a user who was logged in. Since the session is now about to be destroyed you can then safely remove that user from your map/list of logged in users.
}
}
I am having a doubt,i don't know how to get the name of the user using sessionCreated method,...can u pls give me some more code for sessionCreated method!....if u don't mind
Re: Track the user session list
Well sessionCreated is called at the first instance the users session is created, the first time they hit your web application. I dont think you can do anything in there to add the user to a list if you only want to track logged in users.
You need to add a method of your own which adds a user to the list when they have logged in.
However in the method sessionDestroyed you can remove the user from the list.
// Json