Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Practical use of listeners

 
nitin pai
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please mention the use of listeners in practical applications you have used besides being used for logging or testing purposes?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the most common ones is probably the use of ServletContextListener for performing context-wide initializations (what in years past might have been done by a load-at-startup servlet).

SessionListener is very handy for seperating the creation of user sessions from per-user initilization.

Generally, listeners let you separate certain events from actions that should be triggered when those events occur.
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd add "debugging" to that list.

We recently had a web app, where things were being added to the session in many places but not all of them were serializable, leading to a failure.

Not being the original author of this web app, I wrote a SessionAttributeListener which flagged up an error if the added object wasn't serializable.

This certainly sped up the tracing of these illusive objects.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for giving practical use of Listeners.

Nice example Mark!


Thanks,
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!