1. You create 2 instances of X class (call 2x new X()). 2. the valueBound(...) and valueUnbound(...) functions are instance functions.
then: 1. the
causes to call the valueBound on the first instance of X class.
2. the next
causes a call of the valueUnbound(...) of the previous instance of X and a call of the valueBound(...) of the new instance of X.
3. the next
causes a call of the valueUnbound(...) of the instance created in step 2. The new session attribute object, which is a type of String, does not implement the HttpSessionBindingListener interface, so there is call to the valueBound(...) function.
That is all.
Joined: Nov 28, 2004
Thank you for giving such a nice explanation.
Joined: Nov 28, 2004
But according to u, the output should be BUBBUBB(valubeBound,valueUnbound,valueBound,valueUnbound,valueBound).
But the outpur is BBUBUB(valueBound,valueBound,valueUnbound,valueUnbound).
The valueBound method must be called before the object is made available via the getAttribute method of the HttpSession interface. The valueUnbound method must be called after the object is no longer available via the getAttribute method of the HttpSession interface.
So in step 1 ( after First setAttribute method call ) the servlet prints "B", then in next step this SHOULD print "UB" as this get unbound. But the valueBound method of the next step will be called first, and then the valueUnbound method of first will be called.
That's why is the result.
If you put one more similar call to setAttribute, then the result will be B B UB B UB UB
Hope this make sense. ( And I have not confused everybody)
Ha! I have it at last... The answer given is 'BBUBUB'. I too thought that it should be 'BBUBBUBUB'... but if you look closely at the question (original post), its class X that implements the listener - so there is no printout produced for the binding of "x" or the final unbinding of "key"
outputs B again, then UB
outputs UB, and thats it, the binding event of attribute "x" isn't listened for by anything, nor is the final removeAttribute();