Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to know which users are using in the system?

 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i am writing an servelet/jsp application. each user needs
to log into the system with his username/password. if it is
successful, then the application puts a session object whose
attribute name is "user" for each user.
now i need to add a function that allows an administrator user knows
which users are logged in. how can i do it?

thanks!
pete
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this with a Singleton object that has a HashMap of user objects.
You could also use an contexed scoped bean.
The key is the user's session Id.
I use a session listener to add users and remove users from the object as they login/logout.

Of course this can never be completely accurate as some users will just close the browser instead of logging out. In this case, the user will (falsely) appear to be logged in until their session expires.

PS:
Welcome to Java Ranch.
You will soon be asked by the forum moderator to change your name.
<first_name> <space> <last_name>
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben,

thanks for the info and i will do it the same way.
i have updated my publicly displayed name.

regards,
pete
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben,

i tried the HttpSessionListener but it does not work for me.

the two methods in this interface is either called before anything is
put into the session or after all things (such as "user" session object)
in the session are cleared. and so i have no way to know
which user the session belongs to.

i used the HttpSessionAttributeListener and so I know when a "user"
session object is added or removed. however, i dont think it is the
most elegant way.

i am wondering how you did it.

thanks, pete
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container will pass the sessionListener
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionListener.html
a sessionEvent object.
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionEvent.html

That object contains a reference to the session.
If you have the session, you can find out the sessionID.

Use the sessionID as the key and put the session into your HashMap (remember your only storing a reference to the session).

You now have a context scope object with references to all of the session objects (and thus everything they contain).

If you want to see who's logged in, loop through the map and read whatever attribute from each session object you need.
I have a servlet that does this every 5 seconds (using a meta refresh tag).
From a browser, I can sit and watch who's logging in and logging out.
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben,

your method works for me too. it is a clean and elegant solution.

regards,
pete
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you've got it working.


One thing you might want to do, now that you've got it working, is calculate how much time is left in their session. If your session length is 30 minutes and someone hasn't posted for the last 25, you can usually tell that they have closed the browser or just moved on.

The HttpSession object gives you all the methods you need to do this.

[ March 26, 2005: Message edited by: Ben Souther ]
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben,

thanks for the info and i implemented as you described.
i am using tomcat 5.5.7 in development mode. i noticed a strange
situation if you modify and recompile a java program with tamcat
being running. tomcat first removes all sessions via calling the session listener,
and then re-create new sessions objects with same session session IDs,
but this time it does not call registered session listeners when it does this.
so, you will see zero users in your user tracking page.

regards,
pete
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to make sure that your user object (and any objects contained within) implement Serializable. Otherwise they're not going to survive the reload process.
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben,

my object (only a user object with string, date) is serializable.
i still have the same problem. my observation that
the session object (still has the same session id)
is get recreated with tomcat calling the
regestered session listeners and so is the user object.

thanks, pete
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure.
I never reload individual classes in a production environment so I've never run into it.

If you do find anything out, be sure to let me know.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent thread this. I've just implemented it and it works perfectly.

Thank you.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, evidence that someone acutally searched the archives before asking.


Glad it's working for you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's any chance you'll be running across a cluster one day, you might look into a distributed cache or a distributed query of who's on which server. My current environment actually supports both and we sometimes do both to see if the distrbibuted cache is up to date. Look into JMX as a standards-based way to send queries and commands across your cluster.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Glad you've got it working.


One thing you might want to do, now that you've got it working, is calculate how much time is left in their session. If your session length is 30 minutes and someone hasn't posted for the last 25, you can usually tell that they have closed the browser or just moved on.

The HttpSession object gives you all the methods you need to do this.


[ March 26, 2005: Message edited by: Ben Souther ]



Which class do you put this under? The SessionListener is only useful for the starting and ending the session. Where do all event notifications go? Should I just subclass HttpSessionEvent?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the whole thread.
It has a description of how to do this, not code to be cut and pasted.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon my ignorance. Let me rephrase my question. I already implemented the Singleton HashMap solution weeks ago and it works very well. The problem is that the methods in SessionListener only has methods which are called when the session is fresh, and just before its about to terminate. So how can you continue to "check" that a session is idle for a certain amount of time after session creation, but before termination? I can only assume that you have another servlet, which gets the Singleton HashMap, iterates over each session exactly once, calls various methods, and if it thinks the session is stale, invoke invalidate it. Prehaps this runs in another thread?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I see.

I get an instance of the singleton and loop through the sessions from a servlet. The listener is only for adding and removing the sessions.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

How do you "invoke" the servlet? Is it done periodically by a client via a HTTP request? I would have throught that the server would invoke the servlet itself and do a quick iteration through the HashMap and then sleep, then again later after a certain interval. I don't think the client should "poll" it like this.
[ April 16, 2005: Message edited by: Kashif Riaz ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I invoke it with a request.
Whenever I want to see who's in the system (roughly), I hit a servlet with my browser. This servlet iterates through all the sessions, gathers the needed data an prints it to a webpage.
[ April 16, 2005: Message edited by: Ben Souther ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic