Forums Register Login

Attributes Doubt??

+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
yes you are right. Both of them are of String data type.
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
I guess it works like a map. you remove one entry, then put another entry in using the same key.
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 821 times.
Similar Threads
Using EL to generate attribute/value pairs inside of an XHTML element
ServletContext InitParameter Problem
ServletContext events
Need help in XSLT
context getAttribute code doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:56:40.