This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Attributes Doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Attributes Doubt??" Watch "Attributes Doubt??" New topic

Attributes Doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345


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");
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);
27 context.removeAttribute(myAttributeName);
28 context.setAttribute(myAttributeName, null);
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, Certified Scrum Master
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: 9345


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

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

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
I agree. Here's the link:
subject: Attributes Doubt??
Similar Threads
context getAttribute code doubt
ServletContext events
Using EL to generate attribute/value pairs inside of an XHTML element
ServletContext InitParameter Problem
Need help in XSLT