Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retriving attributes using EL

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friends,
please go thru the 2 lines of code and clear my doubt

Somwhere in a servlet i have the following code.........
getServletContext().setAttribute("user","Ram");

Using requestDispatcher i forward the control to a JSP where i hav the following code snippet.........
${applicationScope["user"]}

This code retreives the value Ram correctly,but in HFSJ pg:375,it is given that while retreiving an attribute we should not use " ",while using [] operator.
kindly clarify my doubt...
regards
Raja
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raja,
I am afraid to say that you have misunderstood the material. I request you to go through it again. When you say, ${applicationScope["user"]}, the container will look for a key called user in the map "applicationScope". As you would know, applicationScope is a map of the context-scoped attributes. You could also say ${applicationScope.user}.

If you say ${applicationScope[user]}, it would evaluate user - which means it would look for a scoped attribute named user and use the value of that attribute as the key into the map applicationScope.

${applicationScope[user]} ==> ${applicationScope["Ram"]} ===> null,

unless you have an attribute bound under the name "Ram".
 
Annie McCall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajaraman,

I think it says in HFSJ on page 375 "Without quotes, it will not work". So, you do need quotes. This pertains to Beans and Maps. If it was an ArrayList, you don't need quotes. I think if you read further on page 375, it says you may see something like this:
request.setAttribute("Genre", "Ambient");
Music is ${musicMap[Genre]}.
(This will work since Genre evaluates to a literal value from
request object.)

Hope this helps.
[ August 30, 2005: Message edited by: Ann McCall ]
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sathish BS,Ann McCall
thanx for your explanations.now i am comfortable with the concept....
regards,
Raja
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic