File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How do you evaluate these EL expresions please and why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How do you evaluate these EL expresions please and why" Watch "How do you evaluate these EL expresions please and why" New topic
Author

How do you evaluate these EL expresions please and why

Malika Ben Aziz
Greenhorn

Joined: Oct 26, 2009
Posts: 23




Malika Ben Aziz
Greenhorn

Joined: Oct 26, 2009
Posts: 23
Another question please:

Given this URL
http://com.example/myServlet.jsp?number=three&number=four


Does these EL expressions give the same output?



=



=

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Best way to learn these is by testing these code yourself. And what you don't understand; the operators/keywords used?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Malika Ben Aziz
Greenhorn

Joined: Oct 26, 2009
Posts: 23
Vijitha Kumara wrote:Best way to learn these is by testing these code yourself. And what you don't understand; the operators/keywords used?


I tested the first example



result :
true true

I don't understand why ${not map.d} gives true

map.d is "1", so ${not "1"} should logically give false

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

If I correct, ${not map.d} will evaluate to ${not "1"}, and In logical operations, El treats the unknown variable as false. So not false = true!

Please confirm!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



http://com.example/myServlet.jsp?number=three&number=four









I think, those three are same! paramValues is a Map, on that, number is a key to the parameter List, so we can use them! Please confirm it also!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Abimaran Kugathasan wrote:Please confirm!

Yes. Both are correct.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Vijitha Kumara! Thanks a lot!
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1640
    
  23

Hi Abimaran,
If I correct, ${not map.d} will evaluate to ${not "1"}, and In logical operations, El treats the unknown variable as false. So not false = true!

It is a good explanation, here the lines in the specification to follow:
JSP.2.3.6.2 Unary not operator - {!,not} A
Coerce A to Boolean, apply operator

and if we Coerce A to a Boolean the following applies:
JSP.2.8.5 Coerce A to Boolean
• If A is null or "", return false
• Otherwise, if A is a Boolean, return A
Otherwise. if A is a String, and Boolean.valueOf(A) does not throw an exception return it
• Otherwise, error

So how does Boolean.valueOf() act in case of String which doesn't contain a boolean:
java.lang Class Boolean
public static Boolean valueOf(String s)
Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

It returns false for every String not equal to "true" (ignoring case)

and here you have your proof
Regards,
Frits
Malika Ben Aziz
Greenhorn

Joined: Oct 26, 2009
Posts: 23
Frits Walraven wrote:Hi Abimaran,
If I correct, ${not map.d} will evaluate to ${not "1"}, and In logical operations, El treats the unknown variable as false. So not false = true!

It is a good explanation, here the lines in the specification to follow:
JSP.2.3.6.2 Unary not operator - {!,not} A
Coerce A to Boolean, apply operator

and if we Coerce A to a Boolean the following applies:
JSP.2.8.5 Coerce A to Boolean
• If A is null or "", return false
• Otherwise, if A is a Boolean, return A
Otherwise. if A is a String, and Boolean.valueOf(A) does not throw an exception return it
• Otherwise, error

So how does Boolean.valueOf() act in case of String which doesn't contain a boolean:
java.lang Class Boolean
public static Boolean valueOf(String s)
Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".

It returns false for every String not equal to "true" (ignoring case)

and here you have your proof
Regards,
Frits


Thanks a lot
What surprised me is that it evaluates boolean operators with quotes.
For eg:

${map.a OR map.b} is like ${"true" or "false"} ==> true
${map.a AND map.b} is like ${"true" and "false"} ==> false

So If I understand you, it just knows that the string "true" is evaluated to the boolean true operator
Every thing else with double quotes is "false"

So ${"true" and "false"} is just like ${ true and "unknowStringWhichWillBeFalse"}

Am I correct?


Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks a lot Frits! I just guess it, but you gave the Specification. Thanks for conforming it! My doubt is clear now regarding EL!
Malika Ben Aziz wrote:
So If I understand you, it just knows that the string "true" is evaluated to the boolean true operator
Every thing else with double quotes is "false"

So ${"true" and "false"} is just like ${ true and "unknowStringWhichWillBeFalse"}

Am I correct?


Yea, It's in the Boolean.valueOf(String) method! If anything, other than true, will be false! It'll be safe for us!

Thanks to All!
Malika Ben Aziz
Greenhorn

Joined: Oct 26, 2009
Posts: 23
Abimaran Kugathasan wrote:Thanks a lot Frits! I just guess it, but you gave the Specification. Thanks for conforming it! My doubt is clear now regarding EL!
Malika Ben Aziz wrote:
So If I understand you, it just knows that the string "true" is evaluated to the boolean true operator
Every thing else with double quotes is "false"

So ${"true" and "false"} is just like ${ true and "unknowStringWhichWillBeFalse"}

Am I correct?


Yea, It's in the Boolean.valueOf(String) method! If anything, other than true, will be false! It'll be safe for us!

Thanks to All!


It does the same for primitives


This will print 20


This will print 20 also

${"4" * "5"}

${"4" * "2.5"} ==> 20

I guess it's using Wrappers for everything between " "
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Correct!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do you evaluate these EL expresions please and why
 
Similar Threads
How do i add vendor namespaces using CXF frame work to the SOAP Envvelope
Extract HashMap array value
HashMap
Storing objects in hashmap
Displaying image from database