• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionBindingListener Q

 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following:

public class ServletX extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", "x");
req.getSession().removeAttribute("key");
}
}

and given a Listener class

public class X implements HttpSessionBindingListener
{
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("UB");
}
}

Which logging output would be generated by an invocation of the doGet method?

Given answer: BBUBUB

How is it that value UB is printed twice?
From what I understand, valueUnbound() would be triggered only when an attribute is removed from a session. And that is happening exactly once in the above code snippet
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create two X() s. When the second X() is added to the session with the name "key", the first one in the session is removed.

hope this helps.
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, shouldn't it print BUBBUB?
 
PNS Subramanian
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tested the above code in my env. (Tomcat 5.0.30/Win XP).

The response i get is BBUBUB - binding that takes place prior to removing the already bound object. Thats the behaviour.

Incidentally when i tried changing the value that is bound the response i got was the following

request.getSession().setAttribute("key", new X());
request.getSession().setAttribute("key", new Y());
request.getSession().setAttribute("key", "x");
request.getSession().removeAttribute("key");
X -> B
Y -> B
X -> UB
Y -> UB

Again the key gets bound to a new object and then the old object gets removed.
 
Rajesh Krishnan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that

The first B is for - FirstListener getting bound
second B - SecondListener getting bound
first UB - FirstListener getting unbound
second UB - SecondListener getting unbound

Looks like the secondlistener binding happens first and unbinding of the first happens later.
Try this - u will get a better idea

req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", "x");
req.getSession().removeAttribute("key");
 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", "x");
req.getSession().removeAttribute("key");

i think the reason is
the first bound X(0 cause B
the second cause no output
and the three cause B firstly and UB secondly.
finally the fourth cause UB
so the result is BBUBUB
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I didn't understand why this one

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

didn't get a 'B' printed out?

Thanks,
Jenny
 
Sam Sun
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinghui Yin:
Hi guys,

I didn't understand why this one

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

didn't get a 'B' printed out?

Thanks,
Jenny


"x" isn't X object. Don't have valueBound() function.
[ July 14, 2005: Message edited by: Bruce Sun ]
 
Jingh Yi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bruce. I just realized this kind of listener is the object itself.
 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just the same with the above~~~~
i don't realized the X is the listner itself
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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


I believe that this is what is happening..

req.getSession().setAttribute("key", new X());
It prints "B".
req.getSession().setAttribute("key", new X());
It prints "B".
req.getSession().setAttribute("key", "x");
It prints "UB". (because X object is being removed from session and X is listening)
req.getSession().removeAttribute("key");
It prints "UB".

I have doubt on the last line...How could it print UB when there is no X object bound to session. And when removeAttribute function is called only the simple String object is removed from session.

All the three lines which are setting the attribute "key" in the session, so it will overwrite the previous values in the session.

Please explain

Thanks
Kapil
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not very sure but logically this should be the sequence :


req.getSession().setAttribute("key", new X());-->B

req.getSession().setAttribute("key", new X());--->B
Since this line replaces the previous set "key" in the session it will also print UB because the previously set X object is unbound.

req.getSession().setAttribute("key", "x");
This will print nothing as string x is not a listener.

req.getSession().removeAttribute("key");
This will print UB

Thus the whole sequence will be BBUBUB.

Hope it helps.
[ July 17, 2005: Message edited by: sawan parihar ]
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

req.getSession().setAttribute("key", new X());-->B

req.getSession().setAttribute("key", new X());--->B
Since this line replaces the previous set "key" in the session it will also print UB because the previously set X object is unbound.

req.getSession().setAttribute("key", "x");
This will print nothing as string x is not a listener.

req.getSession().removeAttribute("key");
This will print UB

Thus the whole sequence will be BBUBUB.



req.getSession().setAttribute("key","x"); - this line should print because it replace an X object with a string object.

As in the following line of code replaces an X object with anothere X object and prints UB-

req.getSession().setAttribute("key", new X());-->B
Since this line replaces the previous set "key" in the session it will also print UB because the previously set X object is unbound.


I think, we are still not able to find how this code prints the output.

Kapil
[ July 17, 2005: Message edited by: kapil munjal ]
 
Osama Hasan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is how it goes:

req.getSession().setAttribute("key", new X());----->B

req.getSession().setAttribute("key", new X());----->B
Since this line replaces the previous set "key" in the session it will also print UB because the previously set X object is unbound.

req.getSession().setAttribute("key", "x");
This will print UB since the X object is unbound.

req.getSession().removeAttribute("key");
This wont print anything because the String "x" is removed as a result of this method call.

So the output would be BBUBUB.
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
req.getSession().setAttribute("key","x"); - this line should print because it replace an X object with a string object.


Yes this is right. What Osama Hasan has written is the correct sequence.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kapil munjal:
Hi,

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


I believe that this is what is happening..

req.getSession().setAttribute("key", new X());
It prints "B".
req.getSession().setAttribute("key", new X());
It prints "B".
req.getSession().setAttribute("key", "x");
It prints "UB". (because X object is being removed from session and X is listening)
req.getSession().removeAttribute("key");
It prints "UB".



kapil req.getSession().setAttribute("key", "x"); is causing last UB to be printed and not
req.getSession().removeAttribute("key");
It prints "UB".


please read the above replies... they explained v.nicely..
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

Now it is clear to me how it is printing all these values.

Kapil
 
Senthil Kumar N
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kapil

Rancher's helped a lot things. I think answer given for mock question 55 is wrong. Actual output will be BBUBBUBUB, but in ans it is given as BBUBUB.

how we can arrive is

setAttribute with new Object ---> gives B
setAttribute with new Object ---> gives B
setAttribute ---> gives UB since object already bound to the session so valueUnbound will be called.
then it will call the value bound for
the new value ---> gives B
removeAttribute will call the valueUnbound so it
will print ---> UBUB

so the output will be BBUBBUBUB for clear picture i will put like this to avoid confusion -B--B--UB--B--UB--UB- (bound for "-B-" and for unbound "-UB-".

to get the output of the book answer we should remove one of setAttribute with new Object

please help me out if i have made any error

Thanks

Regards
Senthil
 
Mat Williams
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Senthil,

if you look at carefully at the last setAttribute


you will notice that the attribute being set is not and instance of X but a String object. Thus the answer is correct you will get Bound, Bound, UnBound, UnBound or BBUBUB. This got me thefirst time too, until I thought about it and looked closely.

Mat
 
Senthil Kumar N
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mat,

What you said only I thought, but after executing it in Tomcat I get the out like this BBUBBUBUB(bound bound unbound bound unbound unbound).

For the setAttribute in the api it is given as

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

If the value passed in is null, this has the same effect as calling removeAttribute().

Mat can you clear my doubts clearly. How the flow will be.

Thanks

Regards
Senthil
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Senthil

Read Osama's reply. He has explained clearly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic