Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Application level list of users logged in  RSS feed

 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'd like to create a list of users who are logged into a web application, and store it at the application level. So users who log in get added to the list, and users who log out get removed from the list. Also I'll want to be able to modify information on that list, such as if a user enters or leaves a particular portion of the web application.

So my initial thoughts go to using a singleton (a static instance of an object stored that can only be 'created' by calling a getObject method that returns the static object if it exists or creates a new one if it does not exist). But I'm not sure how to stick it into the web application scope.

Any thoughts on how best to do this?

Thanks!
Skip
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no need to make the object a singleton. Just create one instance and stick it in the application context with ServletContext.setAttribute().
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That sounds good.

But when and where should I create it?

Is that done in a lister? Or should I just check to see if it already exists, and if not then create a new one?

Thanks,
Skip
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sample program that does just this.
http://simple.souther.us/not-so-simple.html
Look for SessionMonitor.
 
Skip Cole
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben!

So you are creating your HashMap in the ContextListener contextInitialized method, and then storing it using context.setAttribute() there.

This looks like a good way to do it.

I was just going to use a Vector, but a HashMap seems also like a good idea.

Best regards,
Skip
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Skip Cole:
Thanks Ben!
I was just going to use a Vector, but a HashMap seems also like a good idea.


In the example, I didn't spend a whole lot of time fussing over synchronization issues for two reasons.

The first being that I wanted to keep things as simple as possible.

The second reason is that you're never going to get a real accurate read on who's actually using your system at any one time anyway. Just because a session is still alive doesn't mean that the user still has your page open in their browser. The screen that shows the sessions gives you the last hit and session start times but it's still up to the user to hit it several times and interpret the results.

Given that, it doesn't (IMHO) make sense to be overly concerned with synchronizing the reads/and writes to the object that keeps track of the sessions.

The methods of the Vector class are synchronized so you might get a little more accuracy by using but I'd prefer to stick with the, newer, collection classes and add the synchronization code myself when needed.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
There's no need to make the object a singleton. Just create one instance and stick it in the application context with ServletContext.setAttribute().


Why cannot we use a singleton for this.I understand your solution but how is that better from the singleton approach.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
Why cannot we use a singleton for this


I didn't say you can't, I said there's no need. Why go through the bother of all that extra code when it's not necessary?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.I though that there is an advantage in the way you have mentioned.

Thanks once again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!