Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get values of all session variable--application wide?

 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a admin jsp page which display all active users at the moment. Is it possible to somehow use session mechnism to implement that?
For example:
User A login with the user name "UA" and that user name is stored in a session variable "loginName". the current time is stored in a session variable "curTime"
User B login with the user name "UB" and that user name is stored in a session variable "loginName". the current time is stored in a session variable "curTime".
User C.....
User D.....
I want the active user 's login name to be displayed:
User currently logined in Logged in Since
--------------------------------- ----------------------
UA .........
UB ........
UC .......
UC ........

Is it possible to implement this without resorting to databse? say using httpsession listener?
Thanks a lot!
RD
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the fundamental point is that session data is accessible only to the client to whom it belongs: there is no global data structure that lists all the active session objects.
You could try a couple of workarounds:
  • Use an HttpSessionListener to store references to all active session objects in a global table. I have a somewhat similar example online in Chapter 10 of http://archive.moreservlets.com/ -- see SessionCounter.java (http://archive.moreservlets.com/Event-Code/events/WEB-INF/classes/moreservlets/listeners/SessionCounter.java)

  • and session-counts.jsp (http://archive.moreservlets.com/Event-Code/events/session-counts.jsp)
  • Use an HttpSessionAttributeListener to record the individual attributes as they are entered.
  • Since you want this data to be global anyhow, forget using the session object and just store it in the servlet context, keyed by user name. After all, the servlet context is where global data is supposed to go.


  • Cheers-
    - Marty
     
    Richard Vagner
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marty,
    Thanks so much for your reply.
    A few questions:
    On the workaround 1 & 2 your suggest:

    1. Does HttpSessionListener require Servlet 2.3. We only have Iplanet6 which supports only Servlet 2.2.
    2. I know your example can record the session count. But how are you going to record the session values: for example if I put loginName="abc" to a session variable, how do I record that attribute ?
    3. When session expires, how are you going to remove it?
    On the workaround 3.
    If the data stored in the servlet context, how do you remove it when session expires?
    Thanks so much
    Richard
     
    Asher Tarnopolski
    Ranch Hand
    Posts: 260
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hey marty,
    your coreservlets/moreservlets books are the best.
    i'm glad i can tell you this "personally"
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!