File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC 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

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.

Peter den Haan
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:
subject: Way to manage sessions as with HttpSessionContext.getSession()?
jQuery in Action, 3rd edition