wood burning stoves 2.0
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Listener question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Listener question" Watch "Listener question" New topic

Listener question

JPraveen Kumar
Ranch Hand

Joined: Aug 31, 2004
Posts: 80
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.
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3322
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.


P.S. Note, since listener is created by servlet container, it must have default (no arguments) public constructor.

Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Exam 1Z0-810: Upgrade to Java SE 8 Programmer Study Guide and Quiz
Omar Dziri

Joined: Oct 06, 2004
Posts: 20
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.

Master degree in computer systems,<br />SCJP 1.4,<br />SCBCD 1.3,<br />SCWCD 1.4,<br />SCDJWS(in progress)
I agree. Here's the link: http://aspose.com/file-tools
subject: Listener question
It's not a secret anymore!