EL Operators

The EL operations are necessary to handle data manipulations. All of the standard and common operators are available. Functionality is included in the EL for relational, arithmetic, and logical operators.

3.3.1 Relational Operators

These operators include: ==, !=, <, >, <=, >=, eq, ne, lt, gt, le, and ge. The last six operators are made available to avoid having to use entity references in XML syntax. Entity references are sometimes required because, if you place a character like < inside an XML element, the parser will throw an error due to its thinking that it's the start of a new element.

All illegal XML characters have to be replaced by entity references. For example, if we had an XML element that looked like:

we would have to replace the < with &lt so it looked like:

<book>if price &lt; 50 then </book>

and could be parsed correctly. If you need to brush up on XML info or how to deal with entity references, visit the tutorial at http://www.w3schools.com/xml/.

The empty prefix operator is also provided for testing whether a value is null or empty meaning"". For example:

<c:if test="${empty param.bookId}">

A book must be selected to process the order </c:if>

3.3.2 Other Operators

In addition to the relational and prefix operators, there are also arithmetic and logical operators that can be used with the EL. Arithmetic operators consist of addition (+), subtraction (-), multiplication (*), division (/ or div), and remainder/modulo (% or mod). Logical operators consist of &&, ||, and !. These represent and, or, and not, respectively.

3.3.3 Using Operators

Some examples of how to use the various operators in expressions are shown in Table 3.2. All of these expressions listed in the table are being shown as they would be used in a test

Expression

Description

Boolean Result

  • 1 == 1}
  • 1 != 1}
  • 1 <= 1}
  • 1 le 1}
  • 1 == 1 || 2 > 3}
  • 1 == 1 and 2 > 3}
  • 6 * 5 == 30}
  • param.name == 'Sue'}
  • empty param.name}
  • not empty param.name && param.name == \"Sue\") and (param.month == 9)}

Equals operator

Not equals operator

Less than or equals

Less than or equals using entity reference

Compound comparison results in (true || false) evaluation Compound comparison results in (true and false)

Multiplication used in comparison results in (30 == 30) Check that request parameter called name is equal to the string Sue. Note the use of single quote around value.

Check for a value of the request parameter called name. If the value is null or "", empty will result in true.

More complex sample of using logical, prefix, and compound expression with parenthesis. If the request parameter called name exists and the value is Sue, and the parameter called month equals 9, then the expression results in true. Note the use of the escape character prior to using double quotes on the param.name comparison. This is an alternative to using single quotes.

True False True True

True

False

True

True, because our JSP is being called with a query string of operators.jsp?name=Sue

False, because our JSP is being called with a query string of operators.jsp?name=Sue

False, because our JSP is being called with a query string of operators.jsp?name=Sue so the expression evaluates to (true && true) and (false) which results in a final value of false.

Table 3.2: Using operators in expressions.

attribute, like:

The evaluation of each expression will result in a Boolean value. For brevity, I only include the expressions (everything between the attribute's quotes) in the table.

Let's make a couple of quick points about working with expressions:

  • The ${} syntax is used for the entire expression, not individual components. This is why we write ${1 == 1 || 2 > 3} not ${1 ==1} || ${2 > 3} or ${ ${1 == 1} or ${2 > 3} }.
  • If you are using && keep in mind that for strict XML documents you need to do &amp&amp.
  • String concatenations do not require the + operator. For example (${param. areacode})-${param.number} x(${param.ext}) would print a string like (303)-5551212 x(11) given the appropriate parameter values.
0 0

Post a comment

  • Receive news updates via email from this site