Link to home
Start Free TrialLog in
Avatar of KPax
KPax

asked on

HttpServlet and ServletRequestAttributeListener explantion

This is from the book I am reading:

Given this code from an otherwise valid HttpServlet that has also been
registered as a ServletRequestAttributeListener:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
         req.setAttribute(“a”, “b”);
         req.setAttribute(“a”, “c”);
          req.removeAttribute(“a”);
}
        public void attributeAdded(ServletRequestAttributeEvent ev) {
        System.out.print(“ A:” + ev.getName() + “->” + ev.getValue());
}
       public void attributeRemoved(ServletRequestAttributeEvent ev) {
       System.out.print(“ M:” + ev.getName() + “->” + ev.getValue());
}
       public void attributeReplaced(ServletRequestAttributeEvent ev) {
       System.out.print(“ P:” + ev.getName() + “->” + ev.getValue());
}

Open in new window


What logging output is generated?

And answer is:
C. A:a->b P:a->b M:a->c

And explanation from book is:
Tricky! The getValue method returns the OLD value of the attribute if the attribute was replaced.

My question is how this could be?
Particularly this part of sequence is not clear to me: P:a->b
Why would it be once again P:a->b instead of P:a->c ?
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KPax
KPax

ASKER

I found this solution in a book  "Servlets and JSP, 2 ed"