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:
[C-sharp] view plain copy < param name= "flashvars" value= "id=1&width=18&height=18" >< param name= "allowscriptaccess" value= "Always" >
- Public class UserObject implements httpsessionbindinglistener{
- public void Valuebound (httpsessionbindingevent event) {
- System. out.println ("Trigger binding event!");
- }
- public void Valueunbound (httpsessionbindingevent event) {
- System. out.println ("Unbind and Session binding");
- }
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.
The 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:
[Java] view plain copy
- MyListener implements httpsessionattributelistener{
- Attributeadded (Httpsessionbindingevenet event) {
- System.out.println ("with object added to the session");
- }
- Attributeremoved (Httpsessionbindingevent event) {
- System.out.println ("The object is removed from the session");
- }
- Attributereplaced (Httpsessionbindingevent event) {
- SYSTEM.OUT.PRINTLN ("attribute value change");
- }
- }
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.
Summarize:
1. Only the class that implements the Httpsessionbindinglistener, triggers its events 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