• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attributes Doubt??

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right. Both of them are of String data type.
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Alex Zhang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it works like a map. you remove one entry, then put another entry in using the same key.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic