Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding getValue() method of HttpSessionBindingEvent class

 
kamalakannan kamal
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 ]
 
sudhakar karnati
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 ]
 
Deepak Chopra
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic