*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on  Listeners in HFSJ-Page 212 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on  Listeners in HFSJ-Page 212" Watch "Doubt on  Listeners in HFSJ-Page 212" New topic
Author

Doubt on Listeners in HFSJ-Page 212

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
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.


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
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

Joined: Aug 02, 2006
Posts: 109
I guess the value that is replaced is printed, b in this case is printed.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Oct 05, 2006
Posts: 434
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on Listeners in HFSJ-Page 212