• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this a bug?

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all.
I've wrote an object that implements HttpSessionBindingListener when I put it on my session using the setAttribute method, the valueBound method was called. Then I put a new object with the same key ( it was equals to the first according to public boolean equals( Object o ) ) and I was expecting that valueUnbound was called followed by valueBound on the new object based on the servlet api that says


public void setAttribute(java.lang.String name,
java.lang.Object value)
Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.


But the replacement never took place.

Is this behavior the same as in other app servers or is a WLS bug?
My code is something like this.

 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryz
The J2EE docs specify in the HttpSessionBindingListener that
This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.
So just because you have replaced the object in session does not mean that the value is Unbound. The values is still bound in session, so the valueUnbound is not called.
As you have quoted
If an object of the same name is already bound to the session, the object is replaced.
The servlet api does not state how your functionality is to be implemented. If however you want the valueUnBound method to be called then remove the old value from the session , which will call valueUnbound and then put the value in session again.
The problem in the above solution is that the valueBound is called again.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
See by implementing interface HttpSessionAttributeListener
There are 3 methods viz- attributeRemoved(HttpSessionBindingEvent event), attributeAdded(HttpSessionBindingEvent event), attributeReplaced(HttpSessionBindingEvent event) .
Here you will realize that if you set the attribute when it is already set then attributeReplace event will be called than attributeRemoved() & attributeAdded().

Originally posted by Zkr Ryz:
Hello all.
I've wrote an object that implements HttpSessionBindingListener when I put it on my session using the setAttribute method, the valueBound method was called. Then I put a new object with the same key ( it was equals to the first according to public boolean equals( Object o ) ) and I was expecting that valueUnbound was called followed by valueBound on the new object based on the servlet api that says

 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both.
So do you say Rahul, that my value is actually being replaced but not unBound from the session ??
The funny thing with this is that if the new Object is not equal to the first one, the value is unBound ( I thought this was a concecuence of being replaced )
So should I understand that if an object is bound to a session and another is stored with the same name the object is replaced but not unBound from the session unleess the new object is not equals ?? Sounds odd to me.
Any way I'll try HttpSessionAttributeListener
that might help me, I understande that this interface is on servlet 2.3, how can I know which servlet version is my WLS running ?

Regards Zkr Ryz
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zkr

Any way I'll try HttpSessionAttributeListener
that might help me, I understande that this interface is on servlet 2.3, how can I know which servlet version is my WLS running ?


Try ServletContext.getMajorVersion()
Returns the major version of the Java Servlet API that this servlet container supports

ServletContext.getMinorVersion()
Returns the minor version of the Servlet API that this servlet container supports.

So do you say Rahul, that my value is actually being replaced but not unBound from the session ??


Yes

The funny thing with this is that if the new Object is not equal to the first one, the value is unBound ( I thought this was a concecuence of being replaced )


Think this is how it is supposed to work. The way you code your equals method is important in this case

So should I understand that if an object is bound to a session and another is stored with the same name the object is replaced but not unBound from the session unleess the new object is not equals ?? Sounds odd to me.


You gessed it right . The HttpSessionAttributeListener is supposed to get your work done. Look at the attributeReplaced event. However for the attributeReplaced to work have a careful look again at your equals() method.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Rahul ;P Got it thanks.
Now one last and finally question . . . .
Is this behavior vendor spefific or is uniform through different app servers ?
Is there a way to know issues like this in advance or I have to wait to test on a different app serv and see it it crashes or not ?
Any way I'll make some tests in the day and I'll let you know, but If you have the answer I would like to hear ( read ) you ....

Bye and have a nice coding.

Originally posted by Rahul Mahindrakar:
Hi Zkr

You gessed it right . The HttpSessionAttributeListener is supposed to get your work done. Look at the attributeReplaced event. However for the attributeReplaced to work have a careful look again at your equals() method.

 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Zkar

Is this behavior vendor spefific or is uniform through different app servers ?


It has to be Uniform as J2EE expects this.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic