This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Trouble understanding evaluation of EL string literal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Trouble understanding evaluation of EL string literal " Watch "Trouble understanding evaluation of EL string literal " New topic
Author

Trouble understanding evaluation of EL string literal

Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
Hi, I'm learning EL and looking at the following example question


The question says will this:

inserted at the end of the above code evaluate to "c"?

From what I've read, a string literal inside the square brackets will be seen as a property name or key.
So I would expect it to return the attribute value which is 1
Then 1 + 1 = 2
And list[2] evaluates to "c"

However this is not the correct answer. Can someone please explain why? I must be missing something important.


SCJP, SCWCD
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1 + 1

'listIdx' is a String. What can you deduce from that ?


[My Blog]
All roads lead to JavaRanch
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1640
    
  23

Hi Doug,

You will get a ELException because the + operator cannot evaluate ${"not a number" + 1}
To get your example working you to use the following line:

Regards,
Frits
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
I'm still a bit confused because in EL I believe that you can do arithmetic operations with strings and conversion goes on behind the scenes.

I can do
${list["1" + 1]}

However having read through your replies and played around with it some more am I right in thinking that in the expression
${list[listIdx + 1]}
listIdx is being evaluated as the attribute value "1" then the addition + is being performed?

but
${list["listIdx" + 1]}
"listIdx" is not returning the attribute value "1" and that's why it fails?

My book says "If what's inside the brackets is... in quotes and it's not an index into an array or List, the Container sees it as the literal name of a property or key"
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1640
    
  23

listIdx is being evaluated as the attribute value "1" then the addition + is being performed?

this is correct.

"listIdx" is not returning the attribute value "1" and that's why it fails?

also correct here.

With an array everything inside the brackets has to evaluate into a number. See the explanation in the book of David Bridgewater:
Arrays, Lists, and Maps
Suppose that I have an array defined as a page attribute
through the following scriptlet:
<% String[] dayArray = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
pageContext.setAttribute("days", dayArray); %>

An expression later in the JSP page source can access the days of the week using
syntax that is practically identical to array syntax. For example, ${days[0]}
will send 'Mon' to page output, while ${days[6]} will send 'Sun.' Within the
expressions curly braces is the name of the attribute (days) followed by square
brackets. Within the square brackets you can place any integer: either a literal or
some attribute that can be sensibly converted to an integer. So if you wrote this code
farther down the page, it would output 'Wed Thu':
<% pageContext.setAttribute("two", new Integer(2));
pageContext.setAttribute("three", "3"); %>
${days[two]}
${days[three]}
The first page attribute, called 'two,' is set to an Integer with a value of 2. So
${days[two]} gets the third value in the array 'Wed.' You can see, though, from
the second attribute, that you do not have to stick with explicit numeric types as with
java.lang.Integer. The second page attribute, called 'three,' still works when loaded
with a String. Provided that a method like Integer.parseInt() can extract an int
value from the String, everything will work.


I hope this helps,

Regards,
Frits
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
OK, I think I've got there!
Thanks Frits
 
Consider Paul's rocket mass heater.
 
subject: Trouble understanding evaluation of EL string literal
 
Similar Threads
Doubt in HFSJ page-420
Doubt in mock question on page no. 433 of Head First Servlets & JSP 2nd edition Chapter 8
Question on EL
EL Coercion question
Expression Language