File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem with valueUnbound() of HttpSessionBindingListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem with valueUnbound() of HttpSessionBindingListener" Watch "Problem with valueUnbound() of HttpSessionBindingListener" New topic
Author

Problem with valueUnbound() of HttpSessionBindingListener

chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
My jsp file is index.jsp:




The "Check.java" serlvlet is:






The listener Dog.java class is ::







The problem is ::


When i send a request to "Check.java" by clicking GO on the "index.jsp" page

The server outputs:

session with id 0e8034ae71b5d0698597ce7beacb has been given a dog Dog@1241039




But, when when i re-send a request to "Check.java" by clicking GO on the "index.jsp" page in the same browser:

The appended output is:

session with id 0e8034ae71b5d0698597ce7beacb has been given a dog Dog@126e598

session with id 0e8034ae71b5d0698597ce7beacb has left a dog null



Can someone please explain the behaviour in the last line, that
HOW THE getValue() METHOD IN THE Dog.valueUnbound() SHOULD RETURN "null"?

I THINK THAT IT SHOULD RETURN "Dog@1241039" BECAUSE THIS WAS THE VALUE UNBOUND ON BINDING EVENT DUE TO SECOND REQUEST.



Thanks,
Chandan Kumar
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Chandan.

When a first request comes to the servlet, container creates a new session object, and stores(bounds) the Dog object in session scope and call's the HttpBindingListener's valueBound() method. In that we get id of the session object and bounded value(i.e Dog object). Finally it sends the 'JSESSIONID' cookie to the client inorder further communicate with it.

When second request comes, container sees the JSESSIONID cookie, and checks is there any session object associated with this id? Here it is there. It will reuse that session object and sets the Dog object into session scope. Here the old Dog object( that we got during first request) will be replaced with new Dog object. New Dog object is added that's why it calls the valueBound() method and got session id and bounded value(i.e new Dog object). Since the old Dog object is removed(i.e unbounded), it calls the valueUnbound() method. Here it displays id of the session object and value(here it will return 'null', since the old Dog object is removed from session scope).
chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
Hi, Chinmaya Chowdary

..... Here it displays id of the session object and value(here it will return 'null', since the old Dog object is removed from session scope)


I am still not clear on the last line in your reply. Would you explain me in detail- how things go on after the first dog object is unbound???
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Chandan mitwaa Kumar wrote:Hi, Chinmaya Chowdary

..... Here it displays id of the session object and value(here it will return 'null', since the old Dog object is removed from session scope)


I am still not clear on the last line in your reply. Would you explain me in detail- how things go on after the first dog object is unbound???



Hi Chandan!


From the way I understood it ...with Chinmaya's explanation...
Here's what's happening.
In the first request you simply set the dog attribute and the valueBound() was called.
In the second request ,you are setting the dog attribute AGAIN. That is , the previous value is getting replaced. And you are getting a new value.
So valueBound() is getting called again.
Then the previous value is unBound().. But since it has already been replaced .. and there is nothing left , you get a null.

Did that make it any clearer?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Nabila,

If you see, the api at the following link, It clearly states that

public Object getValue()

Returns the value of the attribute that has been added, removed or replaced. If the attribute was added (or bound), this is the value of the attribute. If the attribute was removed (or unbound), this is the value of the removed attribute. If the attribute was replaced, this is the old value of the attribute.


As we know that time, the value that was removed was not null so I guess, your explanation for "session with id 0e8034ae71b5d0698597ce7beacb has left a dog null" does look to be inline with what sun says in their apis.


Regards
Salil Verma
Pete Pommelich
Greenhorn

Joined: Oct 14, 2009
Posts: 6
Hi,

are you sure that valueUnbound is called when a attribute is replaced? For me the the part of the API sounds like valueUnbound is only called when the attribute is removed. I tried a little example that seems to confirm this. So I'm wondering why the valueUnBound method in the example above is called. Or am I completely wrong?
chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
Pete Pommelich wrote:Hi,

are you sure that valueUnbound is called when a attribute is replaced? For me the the part of the API sounds like valueUnbound is only called when the attribute is removed. I tried a little example that seems to confirm this. So I'm wondering why the valueUnBound method in the example above is called. Or am I completely wrong?



If you were right, I couldnt have got the following output:


session with id 0e8034ae71b5d0698597ce7beacb has left a dog null
chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
Salil Vverma wrote:Hey Nabila,.....

If you see, the api at the following .....
public Object getValue().....



I also agree on the same explanation as you. What i wonder the output :


session with id 0e8034ae71b5d0698597ce7beacb has left a dog null


I wished someone could help me on the same.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Chandan.

During second request, container creates new Dog object and sets in session scope. Previously there is a session object that is present in the container which is having dog attribute, that object will be replaced with new Dog object. Here replace means the old Dog object is removed and new Dog object is added in session scope. Since our Dog object implements with HttpSessionBindingListener, its valueUnbound() method is called. It displaces the id of the session(since the session object is still alive) and the value of the removed attribute, i.e 'null'.
Pete Pommelich
Greenhorn

Joined: Oct 14, 2009
Posts: 6
Hi, Chandan,

sorry, I made a mistake. The IDE I use for testing didn't print the the exceptions to the output window. So I assumed that the my valueUnbound method was never called.

Kidn Regards
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Chinmaya,

Since our Dog object implements with HttpSessionBindingListener, its valueUnbound() method is called. It displaces the id of the session(since the session object is still alive) and the value of the removed attribute, i.e 'null'.


I think, this is not the right explanation of this behavior reason being the sun says that in valueUnbound() function event_object.getValue() would return the old object if the attribute was replaced.

You can refer the link mentioned in my previous post of the same query. Could you please let us know what I am missing which is creating a difference in my understanding between your explanation and sun's explanation.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
The part of returning previous value - that's gotten me confused!
Though the current value would be null
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Nabila,

As we know that HttpSessionBindingListener is implemented by the object which needs to be tracked about being bound or unbound (in the above example dog is that object). if any object is being removed form session then the valueUnbound() function of the same object (being removed) is called.

In this way I can understand what sun says about getValue(). but I blieve, it has something to do with how HttpSessionBindingEvent object is created.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey All,

we can get the removed or replaced object in the valueUnbound function at least in two ways for sure..

1- this object would return the entry of the object being removed or replaced. -(As in source code 1)
2- In place of replacing use session.revove() and then use session.setAttribute()

I am yet to find out why in the above code, it is giving null.

srouce code 1


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Chandan, which web container are you using ?


[My Blog]
All roads lead to JavaRanch
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Christophe,

Sorry for replying when the question was to chandan but thought to jump in just because I also noticed the same behavior as chandan noticed.

I noticed this behavior in Glassfish application server.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Out put is :


session with id 52CD12E2D3646D467AD17CB3394B43E7 has been given a dog foo.Dog@3c2378

session with id 52CD12E2D3646D467AD17CB3394B43E7 has been given a dog foo.Dog@15311bd


session with id 52CD12E2D3646D467AD17CB3394B43E7 has left a dog null

"this" is foo.Dog@3c2378


First Dog object is foo.Dog@3c2378
Second Dog object replacing the first is foo.Dog@15311bd

argo.value() is Null
and this is foo.Dog@3c2378 (first dog object)


Not sure what's going on here...




D
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I found that the HttpSessionAttributeListener also gives strange results when an attribute is replaced (Tomcat 6.0.16). This must be a container problem.
Somebody also posted it here. Here is what he says :
I can't find any definite statement in the spec regarding the result of HttpSessionBindingEvent.getValue() for each of the events.

But I tested on Tomcat 5.5.27, Tomcat 6.0.18 and Glassfish V2 UR2 Final Build.

The result are consistent for these containers and the statement "returns the old value" is mostly WRONG:

attributeAdded(): value is new value (rather than old value == null)
attributeReplaced(): value is always the NEW value
valueUnbound(): "this" is always the old value. event's value is null iff the attribute has been replaced, or the old value if the attribute has been removed.
attributeRemoved(): indeed the event's value is the OLD value (rather than null)

Who the heck should memorize that?


For the exam, don't trust how these containers behave. Just remember what is written in the spec.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Christophe,

I agree with you. I think, we should remember specification while from SCWCD exam perspective but must know the finer container level details while implementing it in the application.
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

Hi All,

I've tried this example. I changed the source a little but for better understanding.

Check.java


Dog.java


First request:
Value Bound Called, Dog name is: Scooby Doo 0.4296954767014671 isNewSession: true

Second request:
Value Bound Called, Dog name is: Scooby Doo 0.19376960415939726 isNewSession: false
Value UnBound Called, null isNewSession: false

Based on the output, I realized that, by the time valueUnBound called the Dog object has been replaced by the new Dog object.
So, getValue() in the valueUnbound returned 'null'.

Container implementaion is correct


Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hi Sai ,

Let us not rotate the discussion round and round. I guess, this was the first answer from which a detailed discussion evolved. Kindly go through initial few replies and let us know if you are presenting some point which we missed during the discussion and which could bring this discussion to a better end.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with valueUnbound() of HttpSessionBindingListener
 
Similar Threads
HttpSessionBindingListener
need help on question on sessions
listeners problem!
Chapter 6(Session Management) notes (HFSJ) for revision
is this a bug?