File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Mock Exam question 55 - urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock Exam question 55 - urgent" Watch "Mock Exam question 55 - urgent" New topic
Author

Mock Exam question 55 - urgent

Rene Lee
Greenhorn

Joined: Nov 28, 2004
Posts: 4
How is the answer with valueBound first x2 followed by valueUnbound x2, BBUBUB, derived and some explanation why are valueBound and valueUnbound run twice?

here's the question, might have some typo so assume that the code is right.

public class ServletX extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
req.getSession().setAttribute("Key", new X());
req.getSession().setAttribute("Key", new X());
req.getSession().setAttribute("Key", "x");
}
}
public class X implements HttpSessionBindingListener {
public void valueBound(HttpSessionBindingEvent event) {
System.out.print("B");
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.print("UB");
}

}

Thanks
[ November 29, 2004: Message edited by: Rene Lee ]

MCSD, SCWCD 1.4
kishore chitturi
Ranch Hand

Joined: Nov 28, 2004
Posts: 50
Hi,
I also have same doubt.but what i thougt ,when we add a string object to a session,valueBounde method can't be called.
but i am not sure.
can any body help for us.

regards
kishore
Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
Hello,

the explanation is as follows:

1. You create 2 instances of X class (call 2x new X()).
2. the valueBound(...) and valueUnbound(...) functions are instance functions.

then:
1. the

causes to call the valueBound on the first instance of X class.

2. the next

causes a call of the valueUnbound(...) of the previous instance of X and a call of the valueBound(...) of the new instance of X.

3. the next

causes a call of the valueUnbound(...) of the instance created in step 2.
The new session attribute object, which is a type of String, does not implement the HttpSessionBindingListener interface, so there is call to the valueBound(...) function.

That is all.


Gabriel
kishore chitturi
Ranch Hand

Joined: Nov 28, 2004
Posts: 50
Hi Gabrial,

Thank you for giving such a nice explanation.

Regards
kishore
kishore chitturi
Ranch Hand

Joined: Nov 28, 2004
Posts: 50
Hi Gabril,

But according to u, the output should be BUBBUBB(valubeBound,valueUnbound,valueBound,valueUnbound,valueBound).

But the outpur is BBUBUB(valueBound,valueBound,valueUnbound,valueUnbound).

can u explain this.

regards
kishore
Pabak Nanda
Greenhorn

Joined: Nov 04, 2004
Posts: 24
Hello Everybody,

Let me explaine little more on this. In fact Gabriel Forro has already explained the steps.

According to the servlet specs

The valueBound method must be called before the object is made available via the getAttribute method of the HttpSession interface. The valueUnbound method must be called after the object is no longer available via the getAttribute method of the HttpSession interface.


So in step 1 ( after First setAttribute method call ) the servlet prints "B", then in next step this SHOULD print "UB" as this get unbound. But the valueBound method of the next step will be called first, and then the valueUnbound method of first will be called.

That's why is the result.

If you put one more similar call to setAttribute, then the result will be
B
B
UB
B
UB
UB

Hope this make sense. ( And I have not confused everybody)

Thanks,
Pabak :roll:
[ November 29, 2004: Message edited by: Pabak Nanda ]
louise rochford
Ranch Hand

Joined: Apr 04, 2002
Posts: 119
Ha! I have it at last...
The answer given is 'BBUBUB'.
I too thought that it should be 'BBUBBUBUB'... but if you look closely at the question (original post), its class X that implements the listener - so there is no printout produced for the binding of "x" or the final unbinding of "key"

So:

outputs B

outputs B again, then UB

outputs UB, and thats it, the binding event of attribute "x" isn't listened for by anything, nor is the final removeAttribute();

Hope this clears things up at last.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock Exam question 55 - urgent