This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes Way to manage sessions as with HttpSessionContext.getSession()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Way to manage sessions as with HttpSessionContext.getSession()?" Watch "Way to manage sessions as with HttpSessionContext.getSession()?" New topic
Author

Way to manage sessions as with HttpSessionContext.getSession()?

Bill Pearce
Ranch Hand

Joined: Sep 19, 2000
Posts: 53
Hi all,
I am currently working on an application in which a user periodically makes a request to the server to check a flag indicating whether an external event has occurrred. I would like to (when the event does occur) to grab the user's HttpSession and set a flag, so the servlet can simply check for the value there.
The deprecated HttpSessionContext Interface was good for this, as I could get a list of session id's and get a reference to the actual session I wanted. Now that is no more. Has anyone been successful in solving a similar problem? The SessionBindinglistener interface does not seem to give me what I need, as I need to get the session reference when an external event takes place.
Any thoughts appreciated.

Bill
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You might try solving this the other way around.
Use an application-scoped FooBar object that represents whatever needs to be watched. This object would be the observable in the Observer pattern, and java.util.Observable might be a suitable class to use.
Each client that needs to be alerted gets a session-scoped FooBarBean (implements Observer) that registers as an observer with FooBar (Observable.addObserver()). When FooBar updates, it notifies the observers and each observer gets its chance to sniff out what's changed and set whatever flags are necessary.
If it's events rather than updates you're watching for, use java.util.EventListener and java.util.EventObject. The javax.swing.event.EventListenerList support class can be helpful here (G-d only knows why this class is not part of java.util, there's nothing Swing-specific about it). Same idea.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Way to manage sessions as with HttpSessionContext.getSession()?
 
Similar Threads
Stateful Session Bean Usage
2 Listeners - No need to configure in DD
Javascript submit problem
How to block multiple logins of the same user
On Load of every JSF Page