• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam question 55 - urgent

 
Rene Lee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
kishore chitturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kishore chitturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gabrial,

Thank you for giving such a nice explanation.

Regards
kishore
 
kishore chitturi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic