JSP Comparison Operator Behavior

I want to compare two different types in the <c:if> JSP <c:if> . Basically the left one is Number always, but the right is String, and if this string can be parsed for Number, I don't get an error, but if the String argument is not parsed for Number , I get javax.el.ELException: Cannot convert No of type class java.lang.String to class java.lang.Long .

In practice:

$ {1 == "} // works fine
$ {1 == "4"} // works fine $ {1 == "Yes"} // throws an exception.

But even the third comparison worked fine in previous versions of JSP, but now it throws exceptions.

Is == behavior == over a period of time?

Any suggestions are highly appreciated

+7
java spring jsp el
source share
2 answers

The behavior == does not change, but the behavior of {expr} ...

About versions:

In the backward compatibility section of the JSP specification,

If the version specification is less than 2.1, then the syntax {expr} is simply treated as a string literal.

So, until EL 2.0 everything will be considered as a string literal and compared to .equals , since == will be converted to equals internally ( Link here ), but in 2.1. It will not be converted to a string and will throw an exception saying javax.el.ELException: Cannot convert No of type class java.lang.String to class java.lang.Long

About Comparison:

In the JSP specification JSP.2.3.5.7 version EL 2.1, the following is indicated ...

  • If A is null or B is null, return false for == or eq, true for! = Or ne

  • If A or B is Byte, Short, Character, Integer or Long, and B in Long, use the operator

therefore in the first case

 ${1 =="" } // ans is false as second one is null as per 1st rule. 

and in the second case

 ${1 =="4" } // ans is false as both are different after coercing to Long as per 2nd rule. 

Both will be forced to long in the case of internal type conversion.

But not in the third case, ${1 =="Yes" } , where the second line cannot be converted (forced) to Long and java.el.ELException will be passed with the message "It is not possible to convert the class No of type java.lang.String to class java.lang.Long ".

+5
source share

As in JSP 2.1, JSP uses a unified expression language (unified EL), which is a combination of the expression language proposed by JSP 2.0 and the expression language created for JavaServer Faces technology.

It is very likely that the behavior may be slightly different.

See section 1.18 of the JavaServer Pages 2.1 Expression Language Specification (available here ) for all type conversion rules.

+1
source share

All Articles