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:

[C-sharp] view plain copy < param name= "flashvars" value= "id=1&width=18&height=18" >< param name= "allowscriptaccess" value= "Always" >
  1. Public class UserObject implements httpsessionbindinglistener{
  2. public void Valuebound (httpsessionbindingevent event) {
  3. System.      out.println ("Trigger binding event!");
  4. }
  5. public void Valueunbound (httpsessionbindingevent event) {
  6. System.      out.println ("Unbind and Session binding");
  7. }

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
  1. MyListener implements httpsessionattributelistener{
  2. Attributeadded (Httpsessionbindingevenet event) {
  3. System.out.println ("with object added to the session");
  4. }
  5. Attributeremoved (Httpsessionbindingevent event) {
  6. System.out.println ("The object is removed from the session");
  7. }
  8. Attributereplaced (Httpsessionbindingevent event) {
  9. SYSTEM.OUT.PRINTLN ("attribute value change");
  10. }
  11. }

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

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.