aspose file tools*
The moose likes Servlets and the fly likes How to ge know which users are using an app? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to ge know which users are using an app?" Watch "How to ge know which users are using an app?" New topic
Author

How to ge know which users are using an app?

parker yao
Greenhorn

Joined: Sep 20, 2004
Posts: 3
I need to code a web jsp/servlets based web application.
One requriement is to allow an admin to know which users are
in the system now.

What is the elegant way to do this?

Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

When the user logs in, have the app put something in the database that says they are logged in. I usually just use a tiny int with a value of 0 for logged out and 1 for logged in.

Then the admin can just check that field.


GenRocket - Experts at Building Test Data
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30965
    
158

Parker,
I would favor using a session listener and an object in memory. For example, you could use a HashSet with the usernames. You could have a web page that only the administrator has access to. This page would display all the names in the Set.

This solution is more work, but it still works if the administrator doesn't have access to the database. It also handles application crashes better. If the memory object is gone, so are all the sessions. Regardless of which solution you select, make sure you update the value when the session is destroyed (user logged out or session timeout.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Jeanne Boyarsky:
Parker,
I would favor using a session listener and an object in memory. For example, you could use a HashSet with the usernames. You could have a web page that only the administrator has access to. This page would display all the names in the Set.

This solution is more work, but it still works if the administrator doesn't have access to the database. It also handles application crashes better. If the memory object is gone, so are all the sessions. Regardless of which solution you select, make sure you update the value when the session is destroyed (user logged out or session timeout.)


But doesn't each user get their own session? If a user logged in and put his info in the HashSet, how does the Admin's session get ahold of the users session? Sorry if I am just ignorant.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
what about HashMap. like,

hm.put(username, sessionId).

HashSet is better if you want the whole list. we can just put some Bean in it. having attriute like, username, sessionId, etc.

DB is fine for the logging stuff, but for gettin the active users why bother with DB. isn't it time consuming everytime admin wants to view the active users list. and you have to use the Listener in both cases. but yeah you can write some info at login/logout time into DB in order to maintain the log.

we can put that HashMap in Context. when user logs in make an entry to HashMap in sessionCreated() method of the HttpSessionListener, and when logs out delete the entry from HashMap using sessionDestroyed().

please correct me if there would be a problem somewhere.
and one more thing can we destory any session? we have the sessionId.
[ October 03, 2004: Message edited by: adeel ansari ]
Richard Bradford
Ranch Hand

Joined: Apr 20, 2004
Posts: 48
Originally posted by adeel ansari:

and one more thing can we destory any session? we have the sessionId.

[ October 03, 2004: Message edited by: adeel ansari ]

You can no longer get a session object by its id (from servlet 2.1 spec). you could however store the session objects in the HashMap instead of the sessionId.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30965
    
158

But doesn't each user get their own session? If a user logged in and put his info in the HashSet, how does the Admin's session get ahold of the users session?

Gregg,
The HashSet would be global (aka application scope.) Sorry about leaving that out.
parker yao
Greenhorn

Joined: Sep 20, 2004
Posts: 3
Gregg, Jeanne, adeel, and Richard

Thanks so much to all of you for your help here. I think
both approaches have merits and I am going to think
how I can best use them.

Regards, David
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to ge know which users are using an app?