aspose 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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Mock Exam question 55 - urgent" Watch "Mock Exam question 55 - urgent" New topic

Mock Exam question 55 - urgent

Rene Lee

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) {
public void valueUnbound(HttpSessionBindingEvent event) {


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

kishore chitturi
Ranch Hand

Joined: Nov 28, 2004
Posts: 50
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.

Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59

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.

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.

kishore chitturi
Ranch Hand

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

Thank you for giving such a nice explanation.

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.

Pabak Nanda

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

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

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"


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.
I agree. Here's the link:
subject: Mock Exam question 55 - urgent
Similar Threads
Don't understand Final Mock Exam #22, 55
Doubt regarding Listeners
HFSJ Mock Exam Question 55
doubt on HttpSessionBindingListener
Question 55 page 828 HFSJ first edition