• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unbound method is never getting called

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a got some doubts in HttpSessionAttributeListener and HttpSessionBindingListener.

1.when you remove an attribute from a Session will that trigger attributeRemoved() and ValueUnbound() methods.

If so why is that not happening in the below code.

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 class
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 the SessionAttributeListener class

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.


Please clarify this

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should work, I tried similar example like

SessionBindListen ssbl = new SessionBindListen();
SessionBindListen ssb2 = new SessionBindListen();
SessionBindListen ssb3 = new SessionBindListen();
SessionBindListen ssb4 = new SessionBindListen();
sessionObj.setAttribute("hi",ssbl);
sessionObj.setAttribute("hi",ssb2);
sessionObj.setAttribute("hi",ssb3);

sessionObj.setAttribute("hi","hello");
sessionObj.removeAttribute("hi");

sessionObj.setAttribute("X",ssb4);
sessionObj.removeAttribute("X");

and the output was

<<<B>>>Name : hi Value : demo.SessionBindListen@1326484
<<<B>>>Name : hi Value : demo.SessionBindListen@16546ef
<<<UB>>>Name : hi
<<<B>>>Name : hi Value : demo.SessionBindListen@1428ea
<<<UB>>>Name : hi
<<<UB>>>Name : hi

<<<B>>>Name : X Value : demo.SessionBindListen@16d2633
<<<UB>>>Name : X
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My question is when would a unbound() gets called? and why in my case it is not called.And also why my attributeRemoved() method of HttpSessionAttributeListener is not called .Can any one explain the order of how these two listener's methods are triggered.

Bert Bates can you help me out on this.


with thanks,
S.S.S
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Guys can anyone clarify this.
 
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 Senthil Kumar SS:
Hi,
My question is when would a unbound() gets called? and why in my case it is not called.And also why my attributeRemoved() method of HttpSessionAttributeListener is not called .Can any one explain the order of how these two listener's methods are triggered.

Bert Bates can you help me out on this.


with thanks,
S.S.S



Hi,

Your removeAttribute method is never called as you are removing the String Attribute, not of the type SessionAttribute. If you check your log file, the unBound method is throwing exception. You please check your Listener Class design.

Thanks
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic