• 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 on HttpSessionBindingListener

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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 ]
 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

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

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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Deepti Padiyar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 517
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Your Listener Class must have public no argument constructor.

Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic