Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL ${1} Doubt

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q-2 In servlet I wrote
request.setAttribute("1","one");
and In JSP
${1}

but it's printing 1 as output. Why ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because EL interprets it as an Integer.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try ${requestScope["1"]}

Head First explains this on page 377.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe,
Why EL interprets it as an Interger. why not an attribute stored in request scope like it does for others ?

Refer to p-367 and 368...
I am still waiting for the answer
[ March 16, 2008: Message edited by: Sandeep Vaid ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because EL sees 1 as a primitive, not a String.
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe,

in that case does this work {"1"}, hope this is not an integer any more.
clarification please.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case does this work {"1"}

Sorry, it won't work either If you want to use a number, you'd have to go through the requestScope map : ${requestScope["1"]}. By the way, it's not a good idea to use primitives as attribute names
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe,
Why EL interprets it as an Interger. why not an attribute stored in request scope like it does for others ?

Refer to p-367 and 368...



Assume musicMap is a map and Genre is an attribtue. Also 1 is an attribute
${musicMap[Genre]} ----- Evaluates To ---> ${musicMap["Ambient"]}
Then

${musicMap[1]} --> If i have set "1" as an attribtue. Why it doesn't gets evalutes to it's value ?

Do you mean it first check inside [] for an integer (or not) then for whether it is an attribute ?

Why it doesn't treat attributes evaluation Genre and 1 alike ?

I know setting 1 as attribute is not a good idea but the exam may test us on this as it's logically correct....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it doesn't gets evalutes to it's value ?

Because it's an integer, and EL treats primitives as such. The container sees it as a primitive, not as a named variable. If you really want to use an integer, you'll use the requestScope implicit object. What about arrays ? How the container should treat ${myArray[1]} ?
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is the case
I think the book should specify it exactly as :

${1} -----> returns 1 (no matter 1 is set as an attribute}
${a} -----> attributes 'a' value
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic