This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
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 [] operator question

 
Tobin Jackson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I've been trying to get weird with the [] operator. I have the following code in a servlet:

where foodList is an ArrayList with four items. The servlet then forwards the request to a JSP containing the following code:


I thought this should be equivelent to

but i get the following error:

javax.servlet.ServletException: The "[]" operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.

Any ideas?

tobin
 
Tobin Jackson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. been thinking about it some more and I understand that once the translator hits a '${' the rules probably change and there is no scripting allowed but i still can't find anywhere in the JSP 2.0 spec where it says this can't be done. Maybe it's just supposed to be obvious
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy -- I don't have the spec handy, but when the thing inside the brackets is a String literal (in quotes), then it can be one of only these things:

1) A Map key (if the thing to the left is a map)
2) A bean property (if the thing to the left is a bean)
3) An index into a list or array (if the thing to the left is an array or list)

I think that's all you can have... the thing in quotes is never evaluated. But I can't remember how that might be represented in the spec. That spec, as you know, is a little vague in many places

Cheers,
Kathy
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't know anything about the EL but since I'm just browsing through I thought I'd stick my oar in.

Did you try:

i.e. without the quotes? The error you're getting complains that the result of the JSP expression is a String and it's not expecting that. So now you're giving it a String where it is expecting one, albeit perhaps a String literal. Anyway, that syntax doesn't contradict what Kathy's saying, but I won't be holding my breath!

Jules
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tobin Jackson:


I thought this should be equivelent to
Heh. I think it's not what Kathy thinks it is. This is a major difference between JSP 1.2 and JSP 2.0. It is also absolutely horrible code, I hope you realise that! If you use the EL, you should really try to completely avoid scriptlets; mixing the two in a single expression is pure evil

Anyway. You may have used similar techniques successfully in a JSP 1.2 container. To JSP 1.2, an EL expression is not an expression at all, but just a string like any other. The JSTL tags then take this string and interpret it as an expression. This is completely different in JSP 2.0! In JSP 2.0, the expression language is integrated in the container. And according to the specification, JSP.1.3.10.1, the grammar for a chunk of JSP 2.0 body is:

AllBody ::= ( ( '<%=' ExpressionBody ) | ( '${' ELExpressionBody ) | ... )

You are perhaps not able to read BNF grammars, but the basic point is that other than in JSP 1.2, scriptlet expressions and EL expressions are mutually exclusive alternatives. They are peers. In a given expression you can use either the one or the other, but not both at the same time. More formally, the ELExpression production (Expression in JSP.2.9) does not contain either the AllBody or the ExpressionBody productions.

To any fully JSP 2.0 compliant container, "<%= 0 %>" in an EL expression is simply a 7-character EL string literal. The problem is not that it evaluates to a String; the EL will do its best to coerce that String to an integer (JSP.2.8.3). The problem is that "<%= 0 %>" does not at all look like a number and the EL interpreter therefore does not succeed in turning it into the integer it requires at this point. This is exactly the error message you get.

- Peter
[ September 06, 2004: Message edited by: Peter den Haan ]
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter is absolutely right on this one. You cannot mix EL and scripting syntax. PERIOD

These examples have no meaning in JSP v2.0:





-Bryan
 
Tobin Jackson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea, i said originally i was trying to get weird with the [] operator. I would never do that 'cept as an experiment studying for the exam I should have known to look at the BNF in the spec tho.. the answer was right there. Thanks for everyone's input. I feel so EnLightened.

Tobin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic