Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me in EL expression question HFSJ Book page 393

 
Nguyen Hoang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a question:

Given Servlet code:

String num = "2";
request.setAttribute("num", num);
Interger i = new Interger(3);
request.setAttribute("integer", i);

What prints for this expression:
${requestScope[interger] ne 4 and 6 le num || false}

The answer is "false".

But I think that requestScope[integer] will return "null" because there is no request attribute "3" ("3" is the value of integer attribute). So, relational operator can work with null value?
 
Nguyen Hoang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the point!
null will be consider as "0" or "false".
 
John Pearson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right that requestScope[integer] will evaluate to null, but EL is very forgiving in its handling of null values. You'll see on HFSJ page 395 it says "In logical expressions, EL treats the null value as 'false'". That's why the whole expression you gave will evaluate to false.
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for pulling out old threads, but I need clarification regarding the query.

${requestScope[interger] ne 4 and 6 le num || false}

doesn't requestScope[interger] evaluate to 3

Here is my reasoning for answer as 'false'

requestScope[interger] evaluates to 3. Since 3 is not equal to 4, it evaluates to false.

6 le num also evaluates to false since EL does automatic conversion from String to numeric,

the result is: (false and false || false) resulting in output as false.

is my reasoning incorrect. please explain
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
requestScope["interger"] evaluates to 3. The null value occurs because "integer" is not in quotes, so integer is evaluated (as "3"). Since there is no Map entry with "3" as a key, null is returned.

Based on the question it is possible "requestScope[interger]" was not the intended usage.
 
Dee Brown
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I need to correct myself. If the integer attribute evaluates to a numeric value of 3, then requestScope[interger] is not a valid construct because such indexing is only valid for arrays and lists. As previously indicated, I believe requestScope["interger"] was the intended usage.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic