• 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

Doubt regarding Listeners

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

In the HFSJ mock exam 55th question
It is given



and given the listener class



Which logging output would be generated by an invocation
of the doGet method?

a)UBUBUB
b)BBUBUB
c)BBUBUBB
d)BUBBUBB
e)BBUBUBBUB
f)BBUBBUBBUB


Can anybody explain the flow and the answer?



Thanks in Advance..
[ August 08, 2007: Message edited by: Rajesh Kodali ]
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same question when I studied for the exam. What I did was to create the Listener and debug thru the code to see in which order the events occurred. IF you do this it will become clear very quickly what is happening
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Rajesh Kodali",
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
UseRealWords

"reg" is not a word in the English language.


There is also a section on using UBB code tags in there.
If you wrap your posted code in them, the indentation will be preserved making your code easier to read.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajesh,

None of them are correct: ans: BUBBUB

1.req.getSession().setAttribute("key",new X());

adding new X() object results in valueBound So ---> B

2.req.getSession().setAttribute("key",new X());

replaced old X object results in value unbound --> UB
replacing new X() object results in value bound --> B

3.req.getSession().setAttribute("key","x");
"x"(string) reaplces previous X() object. so value unbound called--> UB
 
Raj Menon
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivasan,

I also expected that it works in the same way that you explained.
But in the book the answer is given as

b)BBUBUB
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Answer comes:

BBUBUB

1-valueBound called
2-valueBound called
3-valueUnbound called
4-valueUnbound called


Even if I remove the line req.getSession().removeAttribute("key");
output is same.

Thanks,
[ August 08, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra Bhatt , i did the test in tomcat, and you are right.

But can you explain in detail why the container calls this sequence of methods

Thanks
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i know it now.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristiano Sganzerla,

So you tell us, how is the container calling the methods?


Thanks,
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant the message for Srinivasan. Are you sure?

Rajesh, the book is correct. IF you run the code yourself you will see this to be true
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just ran the test using WebSphere 5.1 server and I got BUBBUB (Bound Unbound Bound Unbound)

I cannot remember what server I used when I was preparing for the exam but I seem to remember that the book was correct.

Is anyone else getting BUBBUB?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it particular container dependent?
I got BBUBUB on tamcat.

Strange!
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tested the same code in both Tomcat and Sun One Application Server 8.2 version. The output is same as what chandra told. It is BBUBUB {Bound Bound Unbound Unbound}



The action happening at each step are :

  • Step 1: You are instantiating a new X and putting it into the session. It will notify the bound() method. So, the answer is B.


  • Step 2: You are instantiating a new X and putting it into the session under the same key "key". This first binds the value to "key" and then unbounds the old value thus notifying bound() and unbound() respectively. So, the answer is BUB.


  • Here i have a doubt. Actually the existing value has to be removed first and then only the new value can be placed right. In such case, the output sequence should have been UBB. But in both Tomcat and SunOne App Server its the reverse as BUB. Am i missing something here?

  • Step 3:

  • Here you are NOT instantiating the X object. Just you are replacing the key "key"'s value with a String object having "x" as its value. But the listener is provided for the X class. So there is no notification. No output as well.

  • Step 4:

  • Here you are removing the String object with the value "x" bound to the key "key". So there is no need to notify.

    Had it been the case of removing the object of type "X", it would have been notified and added one more UB to the output sequence.



    One more info : If you set 'null' for an attribute, its equal to removeAttribute(). That means, if you invoke



    it is equivalent to




    Originally posted by Chandra Bhatt:
    Even if I remove the line req.getSession().removeAttribute("key");
    output is same.



    Chandra, thats because of the key "key" which was having an object of type "X" was already removed with the String value "x" in Step 3. If you just uncomment the step3 and run it, you will get the same.

    Because in this scenario, step3 and step4 are doing the same operation of removing the object of type X already bound to the key "key".

    Hope this clears!
    [ August 08, 2007: Message edited by: Raghavan Muthu ]
     
    Ranch Hand
    Posts: 242
    Mac Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nice explaination Raghavan, kudos

    Am i missing something here?



    You are not missing something. It behaves in the same way.
    See once it will be BUB only not opposite UBB
    See once something bounds then only previous thing unbounds.
    Hence first bounding will be notified and then the unbounding of previous object.
    I think, it makes some sense.
    Try to understand it.

    cheers


    Regards,
    Khushhal
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Originally posted by khushhal yadav :
    See once something bounds then only previous thing unbounds.



    But logically speaking thats NOT right? Without unbounding (taking out) the previous or existing value how can you bound a new value?
     
    khushhal yadav
    Ranch Hand
    Posts: 242
    Mac Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry Raghavan

    But you didn't understand what I did say.

    See an object is eligible for garbage collection, only when it's reference has been assigned to either null or some other object.

    Here I tried to explain the same thing in other context.
    Don't correlate it compeletly with your senario.

    But that's how things actually work.

    Now I think it will make some sense to you.

    Regards,
    Khushhal
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by khushhal yadav:

    See an object is eligible for garbage collection, only when it's reference has been assigned to either null or some other object.



    Thats nice of you kushhal.

    But where does the GC come into the picture? Thought the object being removed would be eligible for GC if at all there are no references for it, but thats totally a different thing. Am i right?

    Its all about removing an attribute from the scope and putting a new attribute (in general, replacing an attribute). To be precise, the order of execution is what we are concerned of here.

    With that context, i expressed my doubt and wanted a clarification.

    Does my question stand valid ?

    I agree there can be a justification saying that whenever you replace an existing value the replacement happens first because of which automatically the existing value gets unbounded. Is that what gonna be the real answer?
     
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I tried this on Tomcat, the result was BBUBUB (Bound Bound Unbound Unbound).
    It seems counterintuitive, I would have guessed Bound Unbound Bound Unbound too if I did not read this thread. Can someone explain this.
    Here is the section from the spec (SRV.7.4) that might be relevant esp the last paragraph.

    Some objects may require notification when they are placed into, or removed
    from, a session. This information can be obtained by having the object implement
    the HttpSessionBindingListener interface. This interface defines the following
    methods that will signal an object being bound into, or being unbound from, a
    session.
    � valueBound
    � valueUnbound
    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.
     
    khushhal yadav
    Ranch Hand
    Posts: 242
    Mac Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Raghavan

    I just tried to explain that in a different context

    Here I tried to explain the same thing in other context.Don't correlate it compeletly with your senario.



    And think you have got that.

    I agree there can be a justification saying that whenever you replace an existing value the replacement happens first because of which automatically the existing value gets unbounded.



    That's the real thing
    And answer to your query.

    Regards,
    Khushhal
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by khushhal yadav:


    That's the real thing
    And answer to your query.



    Thats fine and Thanks Kushhal!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Promod Mahajan:
    I tried this on Tomcat, the result was BBUBUB (Bound Bound Unbound Unbound).
    It seems counterintuitive, I would have guessed Bound Unbound Bound Unbound too if I did not read this thread. Can someone explain this.
    Here is the section from the spec (SRV.7.4) that might be relevant esp the last paragraph.



    Hi Pramod,

    What is your doubt? where you need the explanation? If you could just read my first reply to this post, i think you may get it. Then and there please correlate with this Servlet Spec info.
     
    Michael Ku
    Ranch Hand
    Posts: 510
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There are a lot of explanations here but very few people have actually tried it and posted the results. Would some of you please try it and post the results?

    Thank you
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Michael Ku:
    There are a lot of explanations here but very few people have actually tried it and posted the results. Would some of you please try it and post the results?

    Thank you



    No! You cannot simply say this Michael. Of course i agree with a few people who may be as what you said. But there are many people here who had written books and you cannot add them into your list! Aint I?

    What exactly do you want to be tried out more and results of which to be posted here? we can try.

    If you had read my very first post, i had mentioned that i had tested in both Tomcat and Sun One Application Server. Is that not enough Micahel?
    [ August 09, 2007: Message edited by: Raghavan Muthu ]
     
    Promod kumar
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Michael,
    I did try it and posted my results. Please read my earlier post.

    Raghavan,
    What I am not clear about is what you mentioned as a doubt in your explanation. Why is the valueBound called the second time before the first valueUnbound is called.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Promod Mahajan:

    Raghavan,
    What I am not clear about is what you mentioned as a doubt in your explanation. Why is the valueBound called the second time before the first valueUnbound is called.



    Wow.. there you are! You are also in the list buddy!

    For which also our assumption-cum-conclusion would be the replacement happens first and because of which the existing value gets unbounded. I mentioned in my earlier post anyways.

    Does it help?

    But it would be really nice if some experts could confirm this!
     
    Michael Ku
    Ranch Hand
    Posts: 510
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cristiano, Raghavan, Promod, I did notice that all of you had tried it and got the same answer that the book gave. I was just trying to make the point that there is never a substitute for trying something vs. theorizing what happens (the scientific or empirical method)

    Here is why bound method is called before the unbound method. The following is from the Java Doc for a Hashtable and its put method. Notice that the put returns an object that was replaced.


    put

    public Object put(Object key,
    Object value)

    Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

    The value can be retrieved by calling the get method with a key that is equal to the original key.

    Specified by:
    put in interface Map
    Specified by:
    put in class Dictionary

    Parameters:
    key - the hashtable key.
    value - the value.
    Returns:
    the previous value of the specified key in this hashtable, or null if it did not have one.


    The implementation of the session checks to see if the added object implements the interface and calls bound on it.

    After that the implementation of the session probably checks to see if the returned object implements the interface and then called unBound on it

    Its all in the timing

    However counterintuitive, bound occurs before unbound
     
    Sheriff
    Posts: 14691
    16
    Eclipse IDE VI Editor Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, everybody cool down please

    I was just trying to make the point that there is never a substitute for trying something vs. theorizing what happens (the scientific or empirical method)


    Michael, we understand your intention, and I share that point of view. I also think that there's no better learning than doing things by oneself.
    But please understand that you may also annoy some ranchers by always telling them to "try". I hope you understand
     
    Michael Ku
    Ranch Hand
    Posts: 510
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am probably posing my question poorly. I am confused. I ran a test and got a result that seems wrong.

    I just ran the test using WebSphere 5.1 server and I got BUBBUB (Bound Unbound Bound Unbound)

    I cannot remember what server I used when I was preparing for the exam but I seem to remember that the book was correct.

    Is anyone else getting BUBBUB?



    I am asking for help in understanding why I am getting this result. I do not think this is the correct answer, yet this is what I get when I run the test on WebSphere. has anyone else run this on WebSphere? Is there anyone that would please run this on WebSphere and let me know what the result was?

    Thank you
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Michael Ku:

    has anyone else run this on WebSphere? Is there anyone that would please run this on WebSphere and let me know what the result was?



    Let me try it out on WebSphere and tell you Michael.
     
    Srinivasan thoyyeti
    Ranch Hand
    Posts: 558
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi EveryBody,

    Specification:
    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 specification is not strict enough to guess the correct answer for us.
    Hence we got distinct answers in tomcat,websphere.

    Lets see what container providers has done:

    STEP 1.req.getSession().setAttribute("key",new X());
    After step 1---> value bound. prints B; NO ISSUES.

    STEP 2:req.getSession().setAttribute("key",new X());
    After step 2-->

    prior X object unbound, new X object bound (no order gaurenteed)

    Here specification is not imposed any constraint on order of value bound or unbound.

    valueBound, valueUnBound are executing as atomic transaction.
    and those two are mutually exclusive.
    So no harm in executing them in any order.

    So does the containers
    Tomcat executed: valueBound First
    WebSphere executed: valueUnBound First


    Nothing wrong.
    So there should be two answers for this.

    Have a smile now. Any queries guys.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Srinivasan thoyyeti:

    So specification is not strict enough to guess the correct answer for us.
    Hence we got distinct answers in tomcat,websphere.



    In that case which Web Server we can assume while answering in the exams? Otherwise, the options should contain all possibilities. Is that right?
     
    Srinivasan thoyyeti
    Ranch Hand
    Posts: 558
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes Raghavan,

    If we follow the specification then
    we have the following possibilities

    1. If only one of two(BBUBUB,BUBBUB) present in options ... go for it.
    2. If both(BBUBUB,BUBBUB) are present select both.

    Thats all.
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Srinivasan it looks good, and I am happy with the conclusion as exams are so near

    Raghavan: thanks for asking final doubt (what to select in the exam if
    this like heck question comes)


    Thanks,
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have just now checked with WebSphere Application Server.

    The answer is : BUBBUB {Bound Unbound Bound Unbound} - which matches with our expectation compliant to the logical scenario.

    But it is contradicting with Sun One App Server and Tomcat which give BBUBUB {Bound Bound Unbound unbound} - for the above mentioned 4 steps.

    What should be done for this?

    Experts please help us
    [ August 10, 2007: Message edited by: Raghavan Muthu ]
     
    Michael Ku
    Ranch Hand
    Posts: 510
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Raghavan, thank you for trying this on WebSphere and confirming my findings
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Michael Ku:
    Raghavan, thank you for trying this on WebSphere and confirming my findings



    So, now you agree that i have TRIED out .

    But out of three servers i have tried 2 stand in the same queue but one in a different line for which we need some expert's suggestion here.
     
    Michael Ku
    Ranch Hand
    Posts: 510
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Never doubted that you had tried it, just could not tell if anyone had tried it on WebSphere. As you can see it did made a difference.

    Once again, thank you.

    It is interesting that different servers yield different behavior. I do not think that any of us expected this.
     
    Promod kumar
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is what the documentation says. The way I am reading it is the binding of the new value happens before unbinding of the old value. The reason being the order in which they are mentioned in the documentation first bind, then unbind. Is this right. If so is Websphere not compliant with this spec. I have a hard time believing that.

    Michael,
    Although your point is well taken I think your insistence of the same thing even when people have tried it is really annoying. Please dont take this the wrong way because you add a lot of value to the discussion. Also on the test we do not have the luxury of trying it, so sometimes we answer it and then try it out.

    setAttribute
    public void setAttribute(java.lang.String name,
    java.lang.Object value)Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.
    After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.

    If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.

    If the value passed in is null, this has the same effect as calling removeAttribute().

    Parameters:
    name - the name to which the object is bound; cannot be null
    value - the object to be bound
    Throws:
    IllegalStateException - if this method is called on an invalidated session
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Promod Mahajan:

    Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.



    Thanks Pramod. If we take the order in which it is given in the spec is the same what has been followed by the implementations like Tomcat and Sun One App Server, it is Correct. Means, calling bound and then unbound of the existing object.

    But in that case, Web Sphere does NOT adhere to the Spec or what? Being a J2EE certified Application Server it should be i feel. Is it not?

    I think we come back to the same old question!

    Can people come to a conclusion that the order of execution given in the spec is what going to be implemented by the so-called-container-what-they-imagine-during-the-exam?
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic