For ques 1:
Referring to
Servlet spec.
If an object of the same name is already bound to the session, the object is replaced.
After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.
If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.
Now here is how it goes:
req.getSession.setAttribute("key", new x());
calls valueBound()
req.getSession.setAttribute("key", new x());
calls ValueBound()
The tricky part goes here:
req.getSession.setAttribute("key", "x");
calls valueUnbound()
An object implementing HttpSessionBindingListener (new X()) was bound to name "key" previously, and now an
String object "x"(not implementing the interface) is being bound to name "key", the container will call HttpSessionBindingListener.valueUnbound method on new X() obj(which was bound to name "key").
req.getSession.removeAttribute("key", new x());
calls valueUnbound()
This is best to my understanding, please correct me if I am wrong.
Thanks and regards,
Saurabh Kumar