• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Help me in EL expression question HFSJ Book page 393

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the point!
null will be consider as "0" or "false".
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a tiny ad only because the water is so cold.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic