This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
which interface must a class implement so that instances of the class are notified after any object is added to a session? a, javax.servlet.http.HttpSessionListener b, javax.servlet.http.HttpSessionValueListener c, javax.servlet.http.HttpSessionBindingListener d, javax.servlet.http.HttpSessionAttributeListener
the answer is c,but I think the answer should be d. Am i wrong? the key words "any object" is the class's instance? thanks in advance.
who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
The correct answer would be C. If you read the question statement carefully ,it says that "instances of the class are notified after any object is added to a session" this is the case when the instances are notified whenever they are added or removed to or from any session. In this case the calss,instance of which you are adding as a session attribute should implement HttpSessionBindingListener interfce and should override valueBound(HttpSessionBindingEvent event) and valueUnbound(HttpSessionBindingEvent event) methods and this is the only listener you dont need to declare in DD.
HttpSessionAttributeListener lets you track each time an attribute has been added, removed or replaced from session.This interface is implemented by some other class which is not an "attribute" class. Say for example, Dog is an attribute class* and MyAttributeListener is a class that implements HttpSessionAttributeListener, then MyAttributeListener class will be notified everytime a Dog object is added,removed or replaced from the session.The dog object is never notified of this events.
HttpSessionBindingListener on the other hand lets the objects of the attribute class* to be notified when they are bound (added) to or unbound (removed) from a session..This interface is implemented always by an attribute class*. To make this clear, the attribute class Dog implements HttpSessionBindingListener so that instance of the dog object is notified when any dog object is added to removed from session.
*Attribute class - A class for an object that will be used an attribute value
HttpSessionAttributeListener notifies SESSION about changes to attributes. HttpSessionBindingListener notifies a CLASS INSTANCE about object binding (bound or unbound to session.)
which interface must a class implement so that instances of the class are notified after any object is added to a session?
Since the question is about notifying the instance of the class(Note it's not about notifying session) about object added , I think the correct answer should be c - HttpSessionBindingListener
Hope this helps..!!! [ April 07, 2008: Message edited by: R Nellika ]
Thats correct,if it is really "any" object,the answer will be d. Actually,HttpSessionAttributeListener will be notified whenever "any" object is added or removed from a session,additionally, if any class named as 'A' implements HttpSessionBindingListener and the value which is being bound to the session is an instance of class A,then both the listerners will be notified in this case.so in any case,HttpSessionAttributeListener will always be notified.As far as the above question is concerned,the correct answer will be d.