SCJP/SCWCD
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
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
Kapil Munjal
SCJP 1.4, SCWCD 1.4
Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
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", 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.
Kapil Munjal
SCJP 1.4, SCWCD 1.4
SCJP 1.4<br />SCWCD 1.4
req.getSession().setAttribute("key","x"); - this line should print because it replace an X object with a string object.
Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
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".
Thanks and Regards, Amit Taneja
Kapil Munjal
SCJP 1.4, SCWCD 1.4
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|