• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listener question

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a listener class implemets multiple listener interfaces like ServletContextAttributeListener, HttpSessionAttributeListener then is the same instance used when the different events are triggered or is it a new instance for each event?

Also how can I refer to the instance of the listener object from my servlet code or from other listener code.

Thanks JPraveen.
 
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if listener class implements multiple interfaces, only one instance is created by servlet container and it is used for several types of events.

You can not refer to listener directly (and invoke methods), its life cycle is under control of servlet container, you can only call methods on listener by providing specific events which listener is listening.

regards,
MZ


P.S. Note, since listener is created by servlet container, it must have default (no arguments) public constructor.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Event listeners are instantiated even before the initialization of the servletContext. The web container creates a single instance for each listener class declared in the web.xml file. If the same listener is declared twice in web.xml, two instances are created and so on.
Different events can be triggered to the same listener instance if it implements the appropriate interfaces.
A listener that implements only HttpSessionBindingListener is not required to be declared in web.xml.
reply
    Bookmark Topic Watch Topic
  • New Topic