Httpsessionbindinglistener and Httpsessionattributelistener are two listeners that often confuse beginners, but they are very different. These 2 listeners are referred to in the article as Bindinglistener and Attributelistener.
1. Bindinglistener has 2 methods, Valuebound (Httpsessinbindingevent) and Valueunbount (httpsessionbindingevent). The object that implements the Bindinglistener interface is bound to the session when the Valuebound event is triggered, and the Valueunbound event is triggered when unbound. For example:
1 Public classUserObjectImplementsHttpsessionbindinglistener2 {3 Public voidValuebound (httpsessionbindingevent event)4 {5SYSTEM.OUT.PRINTLN ("Trigger binding event!"));6 }7 Public voidValueunbound (httpsessionbindingevent event)8 {9System.out.println ("Unbind and Session binding"));Ten } One A}Bindinglistener
UserObject user = new UserObject ();
When the listener is saved to the session, the Valuebound event is triggered by Session.setattribute ("user", user).
Session.removeattribute ("user") when the listener is removed from the session, triggering the Valueunbound event; session expiration or timeout
The Valueunbound event is also triggered.
Note: The event is only triggered when the listener (UserObject) is saved to the session or removed from the session, and the event is not triggered when the listener object is saved to the session without implementing it.
2. Attributelistener interface has 3 methods, attributeadded (Httpsessionbindingevent), attributeremoved (httpsessionbindingevent) ,
attributereplaced (httpseesionevent). The corresponding event is triggered when the property value is added, removed, or changed in the session. Example:
1MyListenerImplementsHttpsessionattributelistener2 {3 attributeadded (httpsessionbindingevenet event)4 {5System.out.println ("Object joined in session");6 }7 attributeremoved (httpsessionbindingevent event)8 {9SYSTEM.OUT.PRINTLN ("There are objects removed from the session");Ten } One attributereplaced (httpsessionbindingevent event) A { -System.out.println ("Property value change"); - } the}Attributelistener
Otherobject other = new Otherobject ();
When an object is added to the session, Session.setattribute ("Object", other) triggers the attributeadded event,
When the object is removed from the session, Session.removeattribute ("Object") triggers the attriubteremoved event,
When the value of this property changes, Session.replaceattribute ("Object", another) triggers the attributerepalced event.
Note: Whenever an object is saved to the session or removed from the session or changes the value of the property, the corresponding event is triggered, regardless of whether the object implements Attributelistener.
Summary: 1. Only the class that implements the Httpsessionbindinglistener, the event is triggered when the session is bound and unbound.
2. When Httpsessionattributelistener is implemented, any object, regardless of whether it implements Attributelistener, triggers the corresponding event when it changes.
Httpsessionbindinglistener and Httpsessionattributelistener differences