Httpsessionbindinglistener and Httpsessionattributelistener differences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.