GeeCON Prague 2014*
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
Author

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: 3211
    
    6
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.


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Omar Dziri
Greenhorn

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)
 
GeeCON Prague 2014
 
subject: Listener question