aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock exam doubt from - Head First.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock exam doubt from - Head First.." Watch "Mock exam doubt from - Head First.." New topic
Author

Mock exam doubt from - Head First..

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,

I am having a doubt on one of the mock exams questins for K&B book, question no - 55. Below is the question and the answer.
------------------------------------------------------------
11. public class ServletX extends HttpServlet{
12.public void doGet(HttpServletRequest req,
13.HttpServletResponse resp)
14.throws IOException, ServletException
15.reg.getSession().setAttribute("key",new X());
16.reg.getSession().setAttribute("key",new X());
17.reg.getSession().setAttribute("key","x");
18.reg.getSession().removeAttribute("key");
19. }
20.}

and given a listener class

11.public class X implments HttpSessionBindingListener{
12.public void valueBound(HttpSessionBindingEvent event){
13.System.out.println("B");
14. }
15.public void valueUnbound(HttpSessionBindingEvent event){
16.System.out.println("UB");
17. }
18. }

Ans - BBUBUB


-------------------------------------------------------------
Here my doubt is, when u try to set the value for 'key' at line 16, i assume previous value will be unbound and the new value is bound, is it true? If thats case, will first previous will be unbound and the new value is bound OR, first new value will be bound and the previous value will be unbound?

Thanks,


Narasimha
Nizar Hussein
Ranch Hand

Joined: Oct 23, 2005
Posts: 34
yes, i remember this question when i was ready for the exam, i didnt know if its right or wrong.
it doesn't make any sense.
where are you kathyyyyyyyyyyy???


Nizar Hussein<br /> <br />SCEA(part1), SCDJWS, SCBCD, SCWCD, SCJP
Kuppusamy Venkatasubramanian
Ranch Hand

Joined: Dec 30, 2004
Posts: 91
Hi, I tested by deploying the code and found that the result is
B
B
UB
UB
The reason being,
req.getSession().setAttribute("key",new X()); - This calls ValueBound. So B
req.getSession().setAttribute("key",new X()); - This calls ValueBound. So B
After binding the new value, the old one is unbound. So UB.
req.getSession().setAttribute("key","x"); - This sets the value of key's value to string x. Then value unbound is called on listener X. so UB.

It can evident from the code, if you modify the code as below.

---------------------------------------------------------------------------
package com.saloon;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class ServletX extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException{
req.getSession().setAttribute("key",new X("One"));
req.getSession().setAttribute("key",new X("Two"));
System.out.println(req.getSession().getAttribute("key"));
req.getSession().setAttribute("key","x");
System.out.println(req.getSession().getAttribute("key"));
req.getSession().removeAttribute("key");
}
}
--------------------------------------------------------------------------
package com.saloon;

import javax.servlet.http.*;
import javax.servlet.*;


public class X implements HttpSessionBindingListener
{
String s;
public X(String s)
{
this.s=s;
}
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("B");
}
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("UB");
}
public String toString()
{
return "dog "+s;
}
}
---------------------------------------------------------------------------
B
B
UB
dog Two
UB
x


SCJP1.4, SCBCD 1.3, SCWCD 1.4, SCEA 5, JLPT-N3
My Blog
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Venkatasubramanian,

Thanks for your reply and nice explanation. I am having still one more doubt, will valueUnbound is not called when you remove the attribute from the session? Why none of the methods are called when you call 'req.getSession().removeAttribute("key");' of your code?

Thanks a lot..
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
Hi Venki!

"
req.getSession().setAttribute("key","x"); - This sets the value of key's value to string x. Then value unbound is called on listener X. so UB.
"

In this case, listener will not be called as only a String "x" is added and not an Object of type X(). Hence the last UB is due the removeAttribute.
I suppose Rao's question is also answered here.


SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.
AmitKumar Jain
Ranch Hand

Joined: Jun 13, 2005
Posts: 95
I'd like to add ....
In case of HttpSessionBindingListener , only the objects which implement this listener are notified when they are added/removed from session.
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Thanks Amit,
Now my doubt got clarified...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock exam doubt from - Head First..