| Author |
EL ${1} Doubt
|
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
|
Because EL interprets it as an Integer.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Marc Peabody
pie sneak
Sheriff
Joined: Feb 05, 2003
Posts: 4725
|
|
try ${requestScope["1"]} Head First explains this on page 377.
|
A good workman is known by his tools.
|
 |
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
|
Because EL sees 1 as a primitive, not a String.
|
 |
Garlapati Ravi
Ranch Hand
Joined: Mar 05, 2008
Posts: 168
|
|
Christophe, in that case does this work {"1"}, hope this is not an integer any more. clarification please.
|
Ravi Kumar
SCWCD 5 - 89%, SCJP 1.4 - 90%
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14669
|
|
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
Joined: Feb 27, 2006
Posts: 390
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
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
Joined: Feb 27, 2006
Posts: 390
|
|
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
|
 |
 |
|
|
subject: EL ${1} Doubt
|
|
|