Hey Servlet / JSP campers! We'd like to compile a list of real-world examples of using the various (what are there, about 812?), listener interfaces and classes in the javax.servlet and javax.servlet.http packages. So, for instance, who's ever used HttpSessionAttributeListener, and why did you use it? How about the other 'attribute' listeners? Tell us your stories! (You might get 15 minutes of fame in the upcoming Head First Servlets book!) Thanks, Bert
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I increasingly use ServletContextListeners to load configuration information at server startup. It sure beats the ole Servlet with a load-on-startup=1 trick or at least it feels like less of a hack to me.
Joined: Jul 18, 2001
I've also found HttpSessionListener useful for doing things like tracking which users are currently online.
In one financial planning application we used the HttpSessionListener (specifically on valueUndound when the session died) to record information that a user had entered on a long-running process into a relational database keyed by their userid. Then even though the session died, and they were forced to log back in to the application as a result, we could fetch back their application state and redirect them to the right page to let them continue where they left off. Kyle [ April 09, 2004: Message edited by: Kyle Brown ]
Cool start! I guess I'd agree that HttpSession and ServletContext are probably the most frequently used... Has anyone ever used SessionBinding in real life? How about any of the XxxxxAttribute listeners? Does anybody use SessionActivation? Do you worry about your sessions getting passivated?
I used an HttpSessionBindingListener to maintain a context level record locking device. The basics: user1 opens record1 to edit some information, listener is added to their session and entry made in lock minder. user2 attempts to open record1 to edit some information, lock minder reports record1 is already open for editing, so user2 receives the option to be notified when the record1 becomes available, record opens in read-only mode. user1 logs out/closes record/times out, listener removes entry from lock minder and unlocks record in database (forgot to mention that above...), user2 receives an email letting them know they may now edit the record.