Jean Robillard

Greenhorn

Posts: 17

posted 8 years ago

<% request.setAttribute("bab", ""); %>

${bab eq 0} => outputs false

${bab gt -1} => outputs true

How can I explain this? In a logical expression, an empty string is not treated as 0, because

${6 / bab} => results in an exception, not "Infinity"

How does EL treat an empty string in a logical or arithmetical expression ?

Thanks.

${bab eq 0} => outputs false

${bab gt -1} => outputs true

How can I explain this? In a logical expression, an empty string is not treated as 0, because

${6 / bab} => results in an exception, not "Infinity"

How does EL treat an empty string in a logical or arithmetical expression ?

Thanks.

Charles Lyons

Author

Ranch Hand

Ranch Hand

Posts: 836

posted 8 years ago

Sections 2.3.5 (operators) and 2.8 (type conversions) in the JSP Specification are your friends here.

I'll try to follow them through and see what I get. First ${bab eq 0}. This is of type A == B so section 2.3.5.7 applies. The weakness of the spec here is it doesn't say how an IntegerLiteral (here 0) is to be interpreted -- is it a BigInteger, or long or what? I'll go with BigInteger and see what happens... Since A and B are non-null, and B is an IntegerLiteral (BigInteger?), then type-coerce A into a BigInteger and apply A.equals(B). Section 2.8.3 says "If A is null or empty string, return 0". So now A is 0, a BigInteger (we guess). Then A.equals(B) should return true, since both sides are the BigInteger 0. This also works if IntegerLiteral is supposed to be a Long (say), and we still get true.

Since A is being coerced into a 0 value, the second expression ${bab gt -1} should also return true, which it does in your case.

Finally, ${6 / bab}. Once again, we have a choice -- is an IntegerLiteral a BigInteger or Long? In the first case, we coerce both to BigDecimal and return A.divide(B). This throws ArithmeticException since dividing by 0. try the other way, and coerce both to Double and apply the operator. This time, doing a double division by zero gives, yet again, an ArithmeticException. I assume this is the exception you describe?

So the only result I can't see working is the first -- as far as I can tell, ${ bab eq 0 } should be true. Are you sure that was the EL you tried, and if so what container and version was it on? It could be bug, unless I've misread the specs (and feel free to re-analyse the problem and tell me I made a mistake!).

P.S. So you keep your sanity, I very much doubt these very tricky coercions will appear on the exam!

I'll try to follow them through and see what I get. First ${bab eq 0}. This is of type A == B so section 2.3.5.7 applies. The weakness of the spec here is it doesn't say how an IntegerLiteral (here 0) is to be interpreted -- is it a BigInteger, or long or what? I'll go with BigInteger and see what happens... Since A and B are non-null, and B is an IntegerLiteral (BigInteger?), then type-coerce A into a BigInteger and apply A.equals(B). Section 2.8.3 says "If A is null or empty string, return 0". So now A is 0, a BigInteger (we guess). Then A.equals(B) should return true, since both sides are the BigInteger 0. This also works if IntegerLiteral is supposed to be a Long (say), and we still get true.

Since A is being coerced into a 0 value, the second expression ${bab gt -1} should also return true, which it does in your case.

Finally, ${6 / bab}. Once again, we have a choice -- is an IntegerLiteral a BigInteger or Long? In the first case, we coerce both to BigDecimal and return A.divide(B). This throws ArithmeticException since dividing by 0. try the other way, and coerce both to Double and apply the operator. This time, doing a double division by zero gives, yet again, an ArithmeticException. I assume this is the exception you describe?

So the only result I can't see working is the first -- as far as I can tell, ${ bab eq 0 } should be true. Are you sure that was the EL you tried, and if so what container and version was it on? It could be bug, unless I've misread the specs (and feel free to re-analyse the problem and tell me I made a mistake!).

P.S. So you keep your sanity, I very much doubt these very tricky coercions will appear on the exam!

Charles Lyons

Author

Ranch Hand

Ranch Hand

Posts: 836

posted 8 years ago

Reading that important bit again, I really see no reason why the first statement ${bab eq 0} shouldn't be true. According to the JSP 2.0 spec, the empty string is pretty much treated as 0 in any numerical comparison (by the coercion rules of the operations, it is not 0 in non-numerical comparisons, so you have to be a bit careful there).Section 2.8.3 says "If A is null or empty string, return 0".

Vishal Chugh

Ranch Hand

Posts: 177

posted 8 years ago

Hi Charles,

I have just started practicing EL , i have below JSP :

<%@ page isErrorPage="true" isELIgnored="false" %>

<html>

<body>

<% request.setAttribute("bab", ""); %>

${bab eq 0} </br>

${bab gt -1} </br> </br>

${6 / bab} </br>

${6 + bab} </br> </br>

${bab && true } </br>

${bab && false } </br></br>

${bab || true} </br>

${bab || false} </br>

</body>

</html>

But i get below output as expected by you :

true

true

Infinity

6

false

false

true

false

So we can conclude that :

I have just started practicing EL , i have below JSP :

<%@ page isErrorPage="true" isELIgnored="false" %>

<html>

<body>

<% request.setAttribute("bab", ""); %>

${bab eq 0} </br>

${bab gt -1} </br> </br>

${6 / bab} </br>

${6 + bab} </br> </br>

${bab && true } </br>

${bab && false } </br></br>

${bab || true} </br>

${bab || false} </br>

</body>

</html>

But i get below output as expected by you :

true

true

Infinity

6

false

false

true

false

So we can conclude that :

If A is null or empty string,return 0in case of arithematic and relational operators andfalsein case of logical operaters

Vishal Chugh

Ranch Hand

Posts: 177

posted 8 years ago

Additional :

I have doubt about infinity behaviour in case of relational operators (gt, lt, eq ) , earlier i thought it is being treated as false but ${infinity eq false} gave me false :

For infinity we have below behaviour :

${5} -- 5

${infinity} </br>

${infinity + 5} -- 5

${infinity/5} --0.0

${5/infinity} </br> --infinity

${infinity gt 5}-- false

${infinity lt 5}-- false

${infinity eq 0} </br>-- false

${infinity && true}-- false

${infinity && false} </br>-- false

${infinity || true}--true

${infinity || false}-- false

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]

I have doubt about infinity behaviour in case of relational operators (gt, lt, eq ) , earlier i thought it is being treated as false but ${infinity eq false} gave me false :

For infinity we have below behaviour :

${5} -- 5

${infinity} </br>

${infinity + 5} -- 5

${infinity/5} --0.0

${5/infinity} </br> --infinity

${infinity gt 5}-- false

${infinity lt 5}-- false

${infinity eq 0} </br>-- false

${infinity eq false} -- false

${infinity && true}-- false

${infinity && false} </br>-- false

${infinity || true}--true

${infinity || false}-- false

.In case of arithematic , inifinity act as 0

and

in case of logical (&& ,|| , not) , we can treat infinity as false as per above outputs , Please guide in case of relational operators

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]

Charles Lyons

Author

Ranch Hand

Ranch Hand

Posts: 836

posted 8 years ago

Check your other thread where I explain there is no EL constant called "infinity"---you're in fact referencing a scoped attribute which doesn't exist.

Vishal Chugh

Ranch Hand

Posts: 177

posted 8 years ago

Hi,

Sorry

If i consider infinity as an attribute which doesnt exist , then i agree/disagree for below marked .

${infinity} </br> -Agree

${infinity + 5} -- 5 - Agree , here it will be taken as zero being unknown

${infinity/5} --0.0 -Agree, same reason as above

${5/infinity} </br> --infinity -Agree, same reason as above

${infinity gt 5} -- false - Agree , here it will be taken as zero being unknown

${infinity && true} -- false - Agree , here it will be taken as fasle being unknown

${infinity && false} </br> -- false - Agree , here it will be taken as fasle being unknown

${infinity || true} --true -Agree , here it will be taken as fasle being unknown

${infinity || false} -- false -Agree , here it will be taken as fasle being unknown

I disagree about below :

Please guide

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]

Sorry

If i consider infinity as an attribute which doesnt exist , then i agree/disagree for below marked .

${infinity} </br> -Agree

${infinity + 5} -- 5 - Agree , here it will be taken as zero being unknown

${infinity/5} --0.0 -Agree, same reason as above

${5/infinity} </br> --infinity -Agree, same reason as above

${infinity gt 5} -- false - Agree , here it will be taken as zero being unknown

${infinity && true} -- false - Agree , here it will be taken as fasle being unknown

${infinity && false} </br> -- false - Agree , here it will be taken as fasle being unknown

${infinity || true} --true -Agree , here it will be taken as fasle being unknown

${infinity || false} -- false -Agree , here it will be taken as fasle being unknown

I disagree about below :

${infinity eq 0} </br> -- output is false , it should be true

${infinity eq false} -- output is false , it should be true

${infinity lt 5} -- false - it should be true , as zero less than 5

Please guide

[ December 28, 2008: Message edited by: Vishal Chugh ]

[ December 28, 2008: Message edited by: Vishal Chugh ]