This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HttpSessionBindingListener Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HttpSessionBindingListener Q" Watch "HttpSessionBindingListener Q" New topic
Author

HttpSessionBindingListener Q

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
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

Joined: Jan 25, 2005
Posts: 78
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.


SCJP/SCWCD
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
In that case, shouldn't it print BUBBUB?
PNS Subramanian
Ranch Hand

Joined: Jul 13, 2004
Posts: 150
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

Joined: Jul 14, 2005
Posts: 9
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

Joined: Jan 11, 2005
Posts: 42
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

Joined: Mar 23, 2005
Posts: 90
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

Joined: Feb 01, 2005
Posts: 60
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

Joined: Mar 23, 2005
Posts: 90
Thanks, Bruce. I just realized this kind of listener is the object itself.
air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
just the same with the above~~~~
i don't realized the X is the listner itself
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
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


Kapil Munjal
SCJP 1.4, SCWCD 1.4
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
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 ]

Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
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

Joined: Sep 30, 2002
Posts: 53
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.


SCJP 1.4<br />SCWCD 1.4
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
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

Joined: Mar 14, 2003
Posts: 810
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..


Thanks and Regards, Amit Taneja
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Thanks all,

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

Kapil
Senthil Kumar N
Greenhorn

Joined: Nov 30, 2004
Posts: 19
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

Joined: Jul 20, 2005
Posts: 215
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

Joined: Nov 30, 2004
Posts: 19
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

Joined: Nov 15, 2004
Posts: 1026
Senthil

Read Osama's reply. He has explained clearly.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
 
Consider Paul's rocket mass heater.
 
subject: HttpSessionBindingListener Q