• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock exam doubt from - Head First..

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having a doubt on one of the mock exams questins for K&B book, question no - 55. Below is the question and the answer.
------------------------------------------------------------
11. public class ServletX extends HttpServlet{
12.public void doGet(HttpServletRequest req,
13.HttpServletResponse resp)
14.throws IOException, ServletException
15.reg.getSession().setAttribute("key",new X());
16.reg.getSession().setAttribute("key",new X());
17.reg.getSession().setAttribute("key","x");
18.reg.getSession().removeAttribute("key");
19. }
20.}

and given a listener class

11.public class X implments HttpSessionBindingListener{
12.public void valueBound(HttpSessionBindingEvent event){
13.System.out.println("B");
14. }
15.public void valueUnbound(HttpSessionBindingEvent event){
16.System.out.println("UB");
17. }
18. }

Ans - BBUBUB


-------------------------------------------------------------
Here my doubt is, when u try to set the value for 'key' at line 16, i assume previous value will be unbound and the new value is bound, is it true? If thats case, will first previous will be unbound and the new value is bound OR, first new value will be bound and the previous value will be unbound?

Thanks,
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i remember this question when i was ready for the exam, i didnt know if its right or wrong.
it doesn't make any sense.
where are you kathyyyyyyyyyyy???
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tested by deploying the code and found that the result is
B
B
UB
UB
The reason being,
req.getSession().setAttribute("key",new X()); - This calls ValueBound. So B
req.getSession().setAttribute("key",new X()); - This calls ValueBound. So B
After binding the new value, the old one is unbound. So UB.
req.getSession().setAttribute("key","x"); - This sets the value of key's value to string x. Then value unbound is called on listener X. so UB.

It can evident from the code, if you modify the code as below.

---------------------------------------------------------------------------
package com.saloon;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class ServletX extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException{
req.getSession().setAttribute("key",new X("One"));
req.getSession().setAttribute("key",new X("Two"));
System.out.println(req.getSession().getAttribute("key"));
req.getSession().setAttribute("key","x");
System.out.println(req.getSession().getAttribute("key"));
req.getSession().removeAttribute("key");
}
}
--------------------------------------------------------------------------
package com.saloon;

import javax.servlet.http.*;
import javax.servlet.*;


public class X implements HttpSessionBindingListener
{
String s;
public X(String s)
{
this.s=s;
}
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("B");
}
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("UB");
}
public String toString()
{
return "dog "+s;
}
}
---------------------------------------------------------------------------
B
B
UB
dog Two
UB
x
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venkatasubramanian,

Thanks for your reply and nice explanation. I am having still one more doubt, will valueUnbound is not called when you remove the attribute from the session? Why none of the methods are called when you call 'req.getSession().removeAttribute("key");' of your code?

Thanks a lot..
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venki!

"
req.getSession().setAttribute("key","x"); - This sets the value of key's value to string x. Then value unbound is called on listener X. so UB.
"

In this case, listener will not be called as only a String "x" is added and not an Object of type X(). Hence the last UB is due the removeAttribute.
I suppose Rao's question is also answered here.
 
AmitKumar Jain
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to add ....
In case of HttpSessionBindingListener , only the objects which implement this listener are notified when they are added/removed from session.
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Amit,
Now my doubt got clarified...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic