aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt on HttpSessionBindingListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt on HttpSessionBindingListener" Watch "doubt on HttpSessionBindingListener" New topic
Author

doubt on HttpSessionBindingListener

Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");

public void valueBound(HttpSessionBindingEvent event){
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("UB");
}


answer is BBUBUB

Please explain me ...i have gone through other threads but its not clear enough..thanks


SCJP 1.4<br />SCWCD 1.4(91%)<br />Working on SCJD -Bodgitt & Scrapper Constructions...<br /> <br />"It takes 43 muscles to frown & 17 to smile but it doen't take any to just sit there with a dumb look on your face .. Keep Smiling "
Puneet Vashisht
Ranch Hand

Joined: Jul 30, 2005
Posts: 44
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");

public void valueBound(HttpSessionBindingEvent event){
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("UB");
}


answer is BBUBUB



I think it is quite clear, line 15 and 16 add two attributes, so valueBound() method is called, which prints B couple of times.
With line 17 you replace a value in the attribute, so valueUnbound is called, and it prints UB.
With line 18 you remove the attribute, and so UB gets printed.


The only point important here is that when you replace a value of a attribute, only valueUnbound() is called.


thankz n regards,
puneet
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517


The only point important here is that when you replace a value of a attribute, only valueUnbound() is called.

puneet



Thanks for the information
[ June 25, 2006: Message edited by: Vinod Ennes ]

God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
does it mean that if we replace the value & old and new values are same
(line 15 & 16--BB)then it just calls valueBound only ..but if old and new values are different then its replaced and only unbound is called(UB on line 17..on line 17 its not calling valueBound )
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 90
    
    5

I think it is quite clear, line 15 and 16 add two attributes, so valueBound() method is called, which prints B couple of times.
With line 17 you replace a value in the attribute, so valueUnbound is called, and it prints UB.
With line 18 you remove the attribute, and so UB gets printed.


The only point important here is that when you replace a value of a attribute, only valueUnbound() is called.
[/QB]


No! It's not correct. Line 18 has nothing in common with the subject here. Note that it is HttpSessionBindingListener, not HttpSessionAttributeListener. Therefore it will concern only objects of type X. See this thread


Adam
SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
public class Binding extends HttpServlet implements HttpSessionBindingListener
{

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
request.getSession().setAttribute("name",new Binding());
request.getSession().setAttribute("name",new Binding());
}
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("VALUE BOUND");

}
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("VALUE UNBOUND");
}

}



Below is my output.

VALUE BOUND
VALUE UNBOUND
VALUE BOUND



Can anyone explain this?
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Originally posted by Bhavik Patel:
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");

public void valueBound(HttpSessionBindingEvent event){
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("UB");
}


answer is BBUBUB

Please explain me ...i have gone through other threads but its not clear enough..thanks




I have tried the above code and my output was BUBBUB

I think the code worked like this,

req.getSession().setAttribute("key", new X()) - Wil print B
req.getSession().setAttribute("key", new X());-Wil print UBB
req.getSession().setAttribute("key", "x"); - No effect here,coz 'x' is not an object of this class.

req.getSession().removeAttribute("key") - Wil print UB


So the answer B UBB UB


Corect me if I am wrong.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

I want to make some correction here in vinod's quote

req.getSession().setAttribute("key", new X()) - Wil print B
req.getSession().setAttribute("key", new X());-Wil print UBB
req.getSession().setAttribute("key", "x"); - No effect here,coz 'x' is not an object of this class.

req.getSession().removeAttribute("key") - Wil print UB



req.getSession().setAttribute("key", "x");

Though this statement does not add the attribute which implements the Listener, it is unbinding the attribute of the object which is of type Listener, so the unBound method is called here and print UB.

req.getSession().removeAttribute("key") - will not print anything as it is removing the attribute of type String.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
mid
Greenhorn

Joined: Jun 07, 2006
Posts: 5
Hi,

I want to make a slight correction to this :
--------------------------------------------------------------------
req.getSession().setAttribute("key", new X()) - Wil print B --Fine
req.getSession().setAttribute("key", new X());-Wil print UBB --Fine
req.getSession().setAttribute("key", "x"); - No effect here,coz 'x' is not an object of this class. --Fine

req.getSession().removeAttribute("key") - Wil print UB - ??

How does req.getSession().removeAttribute("key") print UB ...there is no valueunbound or bound here ...it is the key or attribute "key" which is being removed from the session .
Please explain.

Thanks
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Narendra,Mid

I am totally confused.
Have you run the code? . . . What was your output?

Problem is with these two methods

1.req.getSession().setAttribute("key", "x");

As per Narendra the valueUnbound method is called here.
But my understanding is,this will call the attributeAdded() method of the HttpSessionAttributeListener interface.In out context this method wont do anything.The valueUnbound() method will call only if an object of the class type(in which we implemented the HttpSessionBindingListener) is removed from the session.



2.req.getSession().removeAttribute("key")

There is an object 'key' is already in the session scope.Here we are removing the object 'key' from the session.It results invoking valueUnbound() method.



Sorry if my conclusuion is wrong...

Please post your comments.

Thanks
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Originally posted by Vinod Ennes:
public class Binding extends HttpServlet implements HttpSessionBindingListener
{

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
request.getSession().setAttribute("name",new Binding());
request.getSession().setAttribute("name",new Binding());
}
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("VALUE BOUND");

}
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("VALUE UNBOUND");
}

}



Below is my output.

VALUE BOUND
VALUE UNBOUND
VALUE BOUND



Can anyone explain this?


Have you done Refresh page ?. This output is possible If you press the refresh button.
In this case the object is already placed in session scope by first request.
You press the Refresh Button.
The first setAttribute statement cause the valueBound method so print.
VALUE BOUND
At the same time the value is already in the session, so the unBound method is called, so
VALUE BOUND
The second setAttribute statement cause the valueBound method so print.
VALUE BOUND

Make changes in your code to test

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
request.getSession().setAttribute("name",new Binding());
request.getSession().setAttribute("name",new Binding());
request.getSession().setAttribute("name","X");
}

This will give you desired output.

In case of the binding and unbinding
1. The valueBound method of the object added is called first.
2. Then valueUnbound method of the object removed is called.


Hope this help

Thanks
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Originally posted by Bhavik Patel:
15. req.getSession().setAttribute("key", new X());
16. req.getSession().setAttribute("key", new X());
17. req.getSession().setAttribute("key", "x");
18. req.getSession().removeAttribute("key");

public void valueBound(HttpSessionBindingEvent event){
System.out.println("B");
}

public void valueUnbound(HttpSessionBindingEvent event){
System.out.println("UB");
}


answer is BBUBUB

Please explain me ...i have gone through other threads but its not clear enough..thanks




Is the above answer correct?
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Yes, What is the problem with this answer ? Can you figure out.

Thanks
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Originally posted by Narendra Dhande:
Hi,

Yes, What is the problem with this answer ? Can you figure out.

Thanks




Thanks Narendra for your help....I think its better I look into it tomorow.
Deepti Padiyar
Ranch Hand

Joined: Apr 27, 2006
Posts: 31
Hey guys,

lets remove all the confusion... I had to try it myself in a more constructive than destructive way to CLEAR my concepts.I added lots of system.out.prinltn's to make things clear.

here's my code and the output.


and the output.

VALUE BOUND
Added first BindingTest to session attrib key
VALUE BOUND
VALUE UNBOUND
Added second BindingTest to session attrib key
VALUE UNBOUND
Added some String to session attrib key
removes that string value from session attrib key


So my deduction,
First VALUE BOUND is obvious.

While calling second setAttribute on the same key,
for the BindingTest class an instance is bound first and the previous instance is notified of being unbound later (really sad part when u would think otherwise )


When "key" is set to a new String , the second instance of BindingTest is removed from session(unbound) , so VALUE UNBOUND is printed.

And when "key" is removed from session, BindingTest is never notified because there is no instance of it existing in the session.

Hope that makes things clear and less confusing.
[ June 26, 2006: Message edited by: Deepti Padiyar ]

Regards,<br />Deepti.
Deepti Padiyar
Ranch Hand

Joined: Apr 27, 2006
Posts: 31
While running your code Vinod,

VALUE BOUND
Added first BindingTest to session attrib key
VALUE BOUND
VALUE UNBOUND
Added second BindingTest to session attrib key

I always get Bound before Unbound, if you get another answer, i dont know what to think... but my output goes well with the mock exam answers.....

Of course as Narendra says, if you refresh your browser, you will be in the same session with one BindingTest instance in session, so, you will get different results.
So, the next time the result is
VALUE BOUND
VALUE UNBOUND
Added first BindingTest to session attrib key
VALUE BOUND
VALUE UNBOUND
Added second BindingTest to session attrib key

which works with my theory in previous post..
[ June 26, 2006: Message edited by: Deepti Padiyar ]
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Deepti,

I tried your code...
Below is my output....which i copied directly from the console.
I did'nt refresh my page.

VALUE BOUND
Added first Binding to session attrib key
VALUE UNBOUND
VALUE BOUND
Added second BindingTest to session attrib key
VALUE UNBOUND
Added some String to session attrib key
removes that string value from session attrib key


No idea .....
[ June 26, 2006: Message edited by: Vinod Ennes ]
Phil Kurian
Ranch Hand

Joined: Feb 26, 2006
Posts: 35
I haven't tried this out for myself, but let's not forget here that these listener objects run as threads.

If you remember back to when you did you SCJP exam, there is little that can be said about the ordering of threads without synchronization.

Therefore if an attribute is replaced by a new binding object, the result could be unpredictable.

Whilst the valueUnbound may be triggered on the replaced object before the valueBound on the replacement object, it may not be printed in that order. As the valueBound method may get scheduled before the valueUnbound method. That will depend on the thread scheduler.

I will have to verify this for myself, so I urge you to investigate this also, in the chance that I may be wrong.


SCJP (1.4)
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Dudes,
i've tried myself to clarify this out,only to get more confused.

I got a servlet like this
req.getSession().setAttribute("X",new SessionAttribute("Azhar"));
req.getSession().setAttribute("X",new SessionAttribute("Sachin"));
req.getSession().setAttribute("X","senthil");
req.getSession().removeAttribute("X");

and my Binding listener is
class SessionAttribute implements HttpSessionBindingListener
{
SessionAttribute(String name)
{this.name =name;}
String getName()
{return name;}
String name;
public void valueBound(HttpSessionBindingEvent ev)
{ System.out.println("B=>"+ev.getName()+"|"+((SessionAttribute)ev.getValue()).getName());
}

public void valueUnbound(HttpSessionBindingEvent ev)
{
System.out.println("UB=>"+ev.getName()+"|"+((SessionAttribute)ev.getValue()).getName());
}
}


And my SessionAttributeListener code is

public class SessionAttributeListener implements HttpSessionAttributeListener
{
public void attributeAdded(HttpSessionBindingEvent ev)
{
System.out.println("Added=>"+ev.getName()+"|"+((SessionAttribute)ev.getValue()).getName());
}

public void attributeReplaced(HttpSessionBindingEvent ev)
{
System.out.println("Replaced=>"+ev.getName()+"|"+((SessionAttribute)ev.getValue()).getName());
}

public void attributeRemoved(HttpSessionBindingEvent ev)
{
System.out.println("Removed=> "+ev.getName()+"|"+((SessionAttribute)ev.getValue()).getName());
}
}


But the output i'm getting is something awry

B=>X|Azhar
Added=>X|Azhar
B=>X|Sachin
Replaced=>X|Sachin
Replaced=>X|Sachin

why my remove method of HttpSessionAttributeListener and the Unbound method of BindingListener is not getting called.

with regards
S.S.S


when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

req.getSession().removeAttribute("X");

will give you problem, as you are removing the String attribute and refering the sessionAttribute object in the remove method. So there will be ClassclassException.

Other thing I have to test.

Thanks
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Your Listener Class must have public no argument constructor.

Thanks
 
wood burning stoves
 
subject: doubt on HttpSessionBindingListener