It's not a secret anymore!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Small Query regarding EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Small Query regarding EL" Watch "Small Query regarding EL" New topic
Author

Small Query regarding EL

Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Hi I'm preparing for SCWCD.

In HFSJ Head First Servlet and JSP) on page No. 379, They explained that in EL--


This

${musicMap.ambiant}

is the same as this

${musicMap["ambiant"]}

But thi

${musicList["1"]}

CANNOT be turned into this
${musicList.1}

But on the same page in the "Shapen you pencil" code is given as follows:


What would be the out put if following is there in the JSP

${musicList[numbers["2"]]}

the answers is ===> Frou Frou


Can any one please explaing me how the answer "Frou Frou" (It should give an error) could be possible in the context they have explained before the "Sharpen your pencil"?

Thanks in advance.



Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
numbers["2"] cannot be changed to numbers.2, but it is itself a valid expression. In this example (numbers is a List) it will evaluate to an item at index 2, but remember that lists / arrays are idexed 0-based, so this will return the number 3. Then, musicList[3] returns "Frou Frou" (0-based array indexing).
Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
${musicList["1"]}

CANNOT be turned into this
${musicList.1}


okay , the reason for above is that when you use "." operator in EL
1) the left hand side must be a map (except pageContext object) or a attribute (in either one scope)
2) the right hand side must follow Java naming rules


What would be the out put if following is there in the JSP

${musicList[numbers["2"]]}

the answers is ===> Frou Frou


the answer is correct

the step to solve nested expression is start from the most inner portion :
1st,
the numbers["2"], found that numbers is an attribute in request scope (which return an ArrayList) , where the value at index 2 is "3", thus return "3".
Now the EL in the question become ${musicList["3"]}
2nd,
the musicList["3"], found that musicList is an attibute too in request scope (which return an String Array), where the value at index 3 is "Frou Frou", thus reutrn "Frou Frou"

Therefore, "Frou Frou" is the answer.

Remarks : (emphasized in HF book)
(1) "the tricky part is that some of EL looks like Java, but behaves differently. Don't try to map Java language/syntax rules onto EL" (page 369)
(2) make sure you are clear with the differences between "." operator and "[]" operator (page 372)

Hope all this explanation help you !

SCJP 6, SCWCD 5, SCBCD 5
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Thanks Raf for uor explanation.
I understood your point.

But in the explaination before the code i put. for ${musicList["1"]} it would not tuened to ${musicList.1} but as a part of valid expression it should give me an answer "Tahiti 80" in the context of the code.

Am I right?

Thanks



Lee Kian Giap
Ranch Hand

Joined: Jan 23, 2008
Posts: 213
yes, you are right, ${musicList["1"]} will give you answer "Tahiti 80"

I guess that you confuse the purpose of the "Sharpen ..." exercise, those question is to test your understanding on Nested Expression, it does not related to the explanation above this exersice in the same page

and those explanation you provided below actually meant by HF book is

${musicMap.ambiant}

is the same as this

${musicMap["ambiant"]}


you can use either "." operator or "[]" operator for the above EL , both are same
... the HF book doesn't mean that the ${musicMap.ambiant} will turned to this ${musicMap["ambiant"]} when operate


${musicList["1"]}

CANNOT be turned into this
${musicList.1}


you can only use "[]" operator in this situation , not "." operator . As explained in my previous post.
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Thanks Lee for your excellent Explanation!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Small Query regarding EL
 
Similar Threads
using [] operator with an array
Unable to understand the solution on EL
about EL in HFSJ
EL and quotes (HFSJ pg373)
EL Help please