Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you evaluate these EL expresions please and why

 
Malika Ben Aziz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




 
Malika Ben Aziz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best way to learn these is by testing these code yourself. And what you don't understand; the operators/keywords used?
 
Malika Ben Aziz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Please confirm!

Yes. Both are correct.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijitha Kumara! Thanks a lot!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2407
93
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic