my dog learned polymorphism*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Help me in EL expression question HFSJ Book page 393 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Help me in EL expression question HFSJ Book page 393" Watch "Help me in EL expression question HFSJ Book page 393" New topic
Author

Help me in EL expression question HFSJ Book page 393

Nguyen Hoang
Greenhorn

Joined: Mar 24, 2008
Posts: 6
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?


-----------<br />SCJP 1.4<br />SCWCD: On progress...
Nguyen Hoang
Greenhorn

Joined: Mar 24, 2008
Posts: 6
I got the point!
null will be consider as "0" or "false".
John Pearson
Ranch Hand

Joined: Oct 19, 2007
Posts: 51
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

Joined: Jul 14, 2005
Posts: 52
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

Joined: Jun 14, 2008
Posts: 94
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

Joined: Jun 14, 2008
Posts: 94
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me in EL expression question HFSJ Book page 393