File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes page 218 Head First Servlets and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "page 218 Head First Servlets and JSP" Watch "page 218 Head First Servlets and JSP" New topic
Author

page 218 Head First Servlets and JSP

Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Greetings,

I am struggling with Question 8 in the mock exam on that page. I think I am confused about the logical flow of the code. The answer is:

A:a->b P:a->b M:a->c

The code looks like:

...
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());
}

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.

Thank you.

Sincerely,

Jerry Bustamente
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

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 ]

--
Brown belt on KnowledgeBlackBelt.com.
Jerry Bustamente
Ranch Hand

Joined: May 24, 2004
Posts: 90
Francois,

Thank you very much for your guidance.

As a result I see the program flow as:

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.

Any ideas?

Thanks

Jerry Bustamente
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: page 218 Head First Servlets and JSP
 
Similar Threads
getValue() method
Attributes Doubt??
servlet
page no 218 from KathySierra bookabout attributes
Doubt on Listeners in HFSJ-Page 212