• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ final mock question No. 55

 
Rahul Jadaun
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q. No. 55

Can someone explain the answer given. Why wont valueBound() and valueUnbound() be called for a String object being added to session?
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't see the question. But your doubt is more clear to give you an answer.

What I understand from your question is:
- java.lang.String object is added to session.
- Why valueBound() is not called?

My answer for the above question
valueBound() and valueUnbound() are methods of HttpSessionBindingListener and are for ATTRIBUTES THAT IMPLEMENT THIS LISTENER.

java.lang.String doesn't implement this listener and thus these methods are not called when a string is added to a session.
 
Rahul Jadaun
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manikandan..!
That was a silly mistake on my part. I think I have been reading too much.


But here is some addition to the same problem.
Suppose class X implements the binding listener and in a servlet we have
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", new X());
req.getSession().setAttribute("key", "x");
req.getSession().removeAttribute("key");


Now, as far as I get it, when the 2nd object is added, the 1st one will be removed and its valueUnbound() will be called before the valueBound() of the 2nd element. So the order of listener method calls will be -
valueBound(), valueUnbound(), valueBound(), valueUnbound()
But the answer in the book doesn't confirm to this.
Tell me where am I going wrong.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be valueBound valueBound valueUnbound valueUnbound.

coz,


Statement:
Result: valueBound


Statement:
Result: valueBound valueUnbound


Statement:
Result: valueUnBound
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic