Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts in HttpSessionBindingListener

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. My Question is first I am setting the attribute key to the object X() as a value. So if the class X implements HttpSessionBindingListener, then valueBound() will be invoked.

2. Now I am replacing the key value with the same object X(), so the valueBound will be called once again.

3. Now I am replacing the key value with string "x", so this time valueBound won't be called..however possibilty of attributeAdded() if the class implements HttpSessionAttributeListener

4. Here come my Problem: 4. req.getSession().removeAttribute("key");
Now the key is having the string, I am removing it from the session. key is now holding the string, not an object, the how valueUnbound() is called..



ANSWER:
BBUBUB
[ February 14, 2007: Message edited by: Micheal John ]
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

4. Here come my Problem: 4. req.getSession().removeAttribute("key");
Now the key is having the string, I am removing it from the session. key is now holding the string, not an object, the how valueUnbound() is called..


I am really surprised to hear this from a SCJP. If you recall String is an object
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, When 3rd time setAttribute is calling that means replacing the value of from object newX() to String object "x" ... this means that value (new X()) is unbounding.There is no surprise valueUnbound will call.

Here my question is when removeAttribute() is calling.. it is removing the String object not newX() object then how come second UB will call..?

Could you anyone answer this with indetail...?

ThanksinAdvance

Madhav
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ali Gohar:


I am really surprised to hear this from a SCJP. If you recall String is an object


What I thought is whenever, Some object is adding/removing into the session, the valueBound()/valueUnbound will be invoked unless and utill the object implements HttpSessionBindingListener..this is true or not?
 
Tanveer Ahmad
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is what happening with your code. When statement no 1 is executed, the new X() is added so valueBound() is called. When statement no 2 is executed, the new X() is added so valueBound() is called, but this addition causes the removal of previously added X (statement no 1) so the valueUnbaund is called on the first X object. When statement no 3 is executed, the "X" is added which is String so NO valueBound() is called, but this addition causes the removal of previously added X (statement no 2) so the valueUnbaund is called on the SECOND X object. The statement no 4 has no impact on the output as it removes last "X".

Cheers
Tanveer
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your explanation is clear. Thanks a lot!!
 
Tanveer Ahmad
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Some correction... When you over right an object in session what happens is the old object gets removed first (hence the old objects valueUnbaund gets called) and only them new object is added (or overwritten).
So the correct output will be BUBBUB and not BBUBUB.

Cheers
Tanveer.
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tanveer ,

Your confusing,, what is correct ,, they clearly mentioned that answer is BBUBUB ,,,

Then how come your answer,,,?

I think your approach is worng in this case,, Dont you think so,,

Can anyone give more explanation..

Thanks
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSession API only guarantees that valueBound and valueUnbound will be called AFTER the binding methods (ie. setAttribute & removeAttribute) are done. It does not guarantee that valueUnbound() will be called before valueBound() if a new value is replacing another. So either of the outputs are possible.

The API for setAttribute says:

After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.

If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.


One may infer from above that the notifications will be sent in the order they are mentioned above i.e valueBound will be called before valueUnbound. However, it is not very clear.
[ February 15, 2007: Message edited by: Paul Anil ]
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. req.getSession().setAttribute("key", new X());

A new object X is added into the session, and the object X is also implementing HttpSessionBindingListener..so valueBound() is invoked..so it prints B

2. req.getSession().setAttribute("key", new X());

A new object X is now replacing the old one into the session, and hence new object is added/replaced once again, valueBound() is invoked..so it prints B

3. req.getSession().setAttribute("key", "x");

A new string object "x" is now replacing the old object X..and the object X is implementing HttpSessionBindingListener.. that is why the valueUnBound is invoked..so it prints UB

Problematic Area:
4. req.getSession().removeAttribute("key");

Now the key is holding the string object "x", but the String class is not implemeting the HttpSessionBindingListener, then how valueUnbound is invoked?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought Tanveer's reply cleared your doubt when you said
Originally posted by Micheal John:
Your explanation is clear. Thanks a lot!!


So my previous reply was meant for Madhav.

Originally posted by Micheal John:
1. req.getSession().setAttribute("key", new X());

A new object X is added into the session, and the object X is also implementing HttpSessionBindingListener..so valueBound() is invoked..so it prints B

Correct.


2. req.getSession().setAttribute("key", new X());

A new object X is now replacing the old one into the session, and hence new object is added/replaced once again, valueBound() is invoked..so it prints B

Partially correct. Since the old value is removed, valueUnbound() will be called on the original X object, so UB will also be printed. The contention was whether UB will be printed before or after B.


3. req.getSession().setAttribute("key", "x");

A new string object "x" is now replacing the old object X..and the object X is implementing HttpSessionBindingListener.. that is why the valueUnBound is invoked..so it prints UB


Correct.


Problematic Area:
4. req.getSession().removeAttribute("key");

Now the key is holding the string object "x", but the String class is not implemeting the HttpSessionBindingListener, then how valueUnbound is invoked?

Nothing is invoked in this case. The output is because of the previous call.

Try adding the lines one by one and see the output in each case.
 
Tanveer Ahmad
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned by Anil, the order of call is not guaranteed. The only thing assured is that the valueBound and valueUnbound will get called. Weblogic server gave me BUBBUB some, other server will give BBUBUB.
to understand the code better use the following code.


and call following
1. req.getSession().setAttribute("key", new X(1));
2. req.getSession().setAttribute("key", new X(2));
3. req.getSession().setAttribute("key", "x");
4. req.getSession().removeAttribute("key");

Micheal John,
even if youremove the last statement, you will still get the same out put as the last statement has no efeect on the output.

This is very simple. The session is implemented using a hashmap. Now if you add a value using a key which is already present then the old value will get lost.

this could be (COULD BE) the defenition of the setAttribute & removeAttribute method


Now in simple words, a vandor is free to swap the position of "Code segment 1" & "Code segment 2" in setAttribute method of the session class.

I hope I am clear this time.

Cheers
Tanveer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic