This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
If anyone can give me hint I would appreciate it. The book does say, "Tricky! The getValue method retuns the OLD value of the attribute if the attribute was replaced." I'll continue to plow ahead but as I said your guidance is appreciated.
There is a slight semantic difference between the event value given for the method attributeAdded(...) and the methods attributeReplaced(...) and attributeRemoved(...).
On the attributeAdded() event, the attribute name doesn't exist yet in the request scope. So the only value that the event can carry is the new one.
On the attributeRemoved() event, the only value that you can get is the value of the request attribute before it was removed.
On the attributeReplaced() event, you can get the new value by doing request.getAttribute( <attribute_name> ) . So if you wanted to know the old value, the only way to get it is through the event that get passed to the method.
This is why event.getValue() returns the "new" value when passed to the attributeCreated() method and the "old" value when passed to the attributeReplaced() or attributeRemoved() methods.
I hope my explanations will be clear enough.
[ February 27, 2005: Message edited by: Francois Roland ] [ February 27, 2005: Message edited by: Francois Roland ]
Step 1. The first req.setAttribute("a", "b"); invokes attributeAdded to give A:a->b
In this case "a" is the attribute and "b" is the new value.
Step 2. The second req.setAttribute("a", "b"); invokes attributeReplaced to give P:a->b
The container "sees" that the attribute ("a" in this case) already exists so a request to set the attribute is translated into a request to replace the attribute.
In this case "a" is still the attribute and "b" is the old value passed to attributeReplaced.
Step 3. The req.removeAttribute("a");
In this case req.removeAttribute invokes attributeRemoved to give M:a->c
"c" is the new value set by the 2nd setAttribute.
So, two req.setAttribute statements causes the first to set the value and the second to replace the value. The getValue for the 2nd req.setAttribute gives the "old" value.
What I do not understand is the specific mechanism for the 1st setAttribute to invoke attributeAdded while the second setAttribute invokes attributeReplaced. I can see what it does but not how. Likewise, I do not understand how removeAttribute invokes attributeRemoved.