Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

que 22 and 55 in HFS final mock

 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
For Q# 22, i think only A should be the answer. If you are convinced tat the ans given by HFS is true, please explain to me.

Q# 55 talks about listener. I tried this e.g. out and got a different output from all the options stated! Lemme know if you share the same view.
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
Please answer this.

Tx
Kejal
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your inputs still awaited...
 
John Mallavalli
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kejal,
Could you please briefly type in the question # 55 as I don't have the book right now with me? Thanks.
Cheers,
John
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, even i don't have the book rite now, but i'll try.
There's a servlet which has the following code:

session.setAttribute("key", new X());
session.setAttribute("key", new X());
session.setAttribute("key", "value");
session.removeAttribute("key");

public class X implements HttpSessionBindingListener {
public void valueBound(HttpSessionBindingEvent hsbe) {
System.out.println("B");
}
public void valueUnbound(HttpSessionBindingEvent hsbe) {
System.out.println("UB");
}
}

there were some options with sequence of B and UB. I tried this, but found an output which wasn't in any of the options.
 
John Mallavalli
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, there is a problem with this question. I remember it now.
- John
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kejal,

Please look at this thread regarding Q 22.
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate,
I had already referred to it
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About Q55.
Did you try to create and debug this question?
I did, and all things are pretty obvious.

Small notice: valueBound() called BEFORE the object will be set as attribute to the session. It gives the possibility to the object to sinchronize itself let say with the DB.

When the first session.setAttribute("key", new X()) method is called, BEFORE the object is added to the session the object's X valueBound() method is called. (We have B printed)

When the second session.setAttribute("key", new X()) is called, the object's X valueBound() method is called (we have BB printed). But object is not added to the session at this time. After method valueBound() comleted, container tries to add this object to the session and sees that the object with the same key has already existed. In this case container removes the old one attribute from the session and of course the object's X method valueUnbound() is caled (we have BBUB printed).

And in the end the session.setAttribute("key", "x") is called. The value is just plain old String not the X class object. String class does not implements the HttpSessionBinding listener. So when container tries to set the "x" value as the session attribute it must first remove old one attribute with the same key. In this case only valueUnbound() is caled (we have BBUBUB printed);

When the session.semoveAttribute("key") is called, container does not has deal with the object of type X because the last added attribute was of type String. So no methods of object's X is called.

That's all.
Hope it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic