Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Listeners in HFSJ-Page 212

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am unable to understand the solution for the above question. The question is like this:

Given this code from an otherwise valid HttpServleet 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());
}



Which logging output is generated?

The answer is A:a->b P:a->b M:a->c


Now, I am unable to understand how the answer has come. Can you please explain?


With regards,
Padma priya N.G.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,



#1: attribute "a" with value is added to the request scope causing
method attributeAdded() called.
#2: value of attribute "a" (that is "b")is replaced with value "c"
causing method attributeReplaced() called.
#3: Finally attribute "a" is removed from the request scope
causing method attributeRemoved() called.

Thanks,
[ July 19, 2007: Message edited by: Chandra Bhatt ]
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the attributeRemoved, b is replaced by c. But, P:a->b is printed and why it is not P:a->c?
With regards,
Padma priya N.G.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the value that is replaced is printed, b in this case is printed.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya:

Straight from the Servlet 2.4 Specification:


getName()
public java.lang.String getName()
Return the name of the attribute that changed on the ServletRequest
Returns: the name of the changed request attribute

getValue()
public java.lang.Object getValue()
Returns the value of the attribute that has been added, removed or replaced.
If the attribute was added, this is the value of the attribute. If the
attribute was removed, this is the value of the removed attribute. If the
attribute was replaced, this is the old value of the attribute.



Lines marked bold is answer to your doubt specifically.

Thanks,
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic