Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt regarding getValue() method of HttpSessionBindingEvent class

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Please look into the following code and clear my doubt.

Servlet code

public class SessionServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession();
SessionObject obj1 = new SessionObject("One");
SessionObject obj2 = new SessionObject("Two");
session.setAttribute("Attribute",obj1);
session.setAttribute("Attribute",obj2);
}
}

class SessionObject implements HttpSessionBindingListener
{
private String data;

SessionObject(String data){ this.data = data;}

public String toString(){ return data; }

public void valueBound(HttpSessionBindingEvent event){}

public void valueUnbound(HttpSessionBindingEvent event){}
}

-----------------------------------------------------------------------------------


HttpSession Attribute Listener

public class AttributeListener implements HttpSessionAttributeListener
{
public void attributeAdded(HttpSessionBindingEvent event){}

public void attributeReplaced(HttpSessionBindingEvent event)
{
System.out.println("event.getValue());
}

public void attributeRemoved(HttpSessionBindingEvent event){}
}

-------------------------------------------------------------------------------------

The getValue() method of HttpSessionBindingEvent in above program outputs "Two" not "One". Servlet specification says that this method returns the value of the attribute that has been added, removed or replaced. If the attribute was added (or bound), this is the value of the attribute. If the attribute was removed (or unbound), this is the value of the removed attribute. If the attribute was replaced, this is the old value of the attribute."

Here the old value is "One" but it's returning "Two" which is new value. But if SessionObject class in above program did not implement
HttpSessionBindingListener interface, than the output is "One" as expected.

Can anybody explain why this is happening?
[ July 09, 2008: Message edited by: kamalakannan kamal ]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even i am getting the same result..

can anybody explain this issue..

Thanks & Regards,
Sudhakar Karnati
[ July 09, 2008: Message edited by: sudhakar karnati ]
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is following the below sequence:

1) Called the valueBound sequence of obj1 --> BindingListenerEvent.getValue()--> obj1.toString()

2) Called the attributeAdded for obj1

3) Called the valueUnbound of obj1

4) Called the valueBound of obj2 --> BindingListenerEvent.getValue()--> obj2.toString()

5) Called the attributeReplaced for obj1,obj2 --> event.getValue() --> obj2.toString()
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic