Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EL problem  RSS feed

 
Anna Wang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone:

When I ran this JSP code, I expected it will output 14, but the result is 4. Can anybody explain a little bit? Thanks.

<%
String s = "10";
%>
${4+s}

=============

Later I change code to:
${4 + "10" }
it outputs 14, what's different about those two codes?
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you mention ${s} then it uses pageContext.findAttribute() API to search for a variable called s in any of the scopes. Since no such attribute exists (you have created a scripting variable here) it returns "" .

And thus you get 4 only.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!