| Author |
Attributes Doubt??
|
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Guys, I have the doubt in the code below that I got from David Bridgewater's book, 10 // "out" is the response's PrintWriter 11 out.write("<H2>Context (Application) Scope</H2>"); 12 ServletContext context = getServletContext(); 13 String myAttributeName = "com.osborne.conductor"; 14 context.setAttribute(myAttributeName, "Andre Previn"); 15 16 enum = context.getAttributeNames(); 17 while (enum.hasMoreElements()) { 18 attrName = (String) enum.nextElement(); 19 attrValue = context.getAttribute(attrName); 20 out.write("<BR />Attribute name: <B>" + attrName + "</B>, value: <B>" 21 + attrValue + "</B>"); 22 } 23 String conductor = (String) context.getAttribute(myAttributeName); 24 out.write("<BR /> Just used getAttribute() to obtain " 25 + myAttributeName + " whose value is " + conductor); 26 27 context.removeAttribute(myAttributeName); 28 context.setAttribute(myAttributeName, null); 29 30 out.write("<BR />Value of attribute " + myAttributeName + " is now " 31 + context.getAttribute(myAttributeName)); I want to know what like 18 and 19 does??
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
Micheal John
Ranch Hand
Joined: Nov 01, 2006
Posts: 344
|
|
|
getAttributeName() return type is enumeration. The line 18 and 19 does the iteration and get all the attributes in the context scope one by one..
|
Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Michael, My doubt is that, in the book they did not show what type the attrName and attrValue are... They must be Strings right?? I got this one. Thanks!
|
 |
Gopikrishna krishna
Greenhorn
Joined: Feb 21, 2007
Posts: 1
|
|
|
yes you are right. Both of them are of String data type.
|
 |
Swati Udas
Ranch Hand
Joined: Aug 18, 2004
Posts: 121
|
|
Regarding the code here I have a doubt about line 27 and 28.. Can we set an attribute to null when its already been removed in previous line ?
|
SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
|
 |
Alex Zhang
Greenhorn
Joined: Feb 21, 2007
Posts: 3
|
|
|
I guess it works like a map. you remove one entry, then put another entry in using the same key.
|
Ctrl Space Enter
|
 |
 |
|
|
subject: Attributes Doubt??
|
|
|