Java.Servlet.What types of EL operators do you know?

In EL (${ ... }), you can use many types of operators — very similar to Java, but slightly simplified.

Here’s the full organized list:


🔵 1. Arithmetic Operators

OperatorDescriptionExample
+Addition${2 + 3}5
-Subtraction${5 - 2}3
*Multiplication${4 * 2}8
/ or divDivision${10 / 2} or ${10 div 2}5
% or modModulus (remainder)${10 % 3} or ${10 mod 3}1

div and mod are alternative keywords for / and % (useful when writing XML-compliant EL inside XHTML).

🔵 2. Relational (Comparison) Operators

OperatorDescriptionExample
== or eqEqual to${5 == 5} or ${5 eq 5}true
!= or neNot equal to${5 != 3} or ${5 ne 3}true
< or ltLess than${3 < 5} or ${3 lt 5}true
> or gtGreater than${5 > 3} or ${5 gt 3}true
<= or leLess than or equal to${5 <= 5} or ${5 le 5}true
>= or geGreater than or equal to${5 >= 3} or ${5 ge 3}true

✅ Again, the text forms like eq, lt, gt are XML-friendly.

🔵 3. Logical Operators

OperatorDescriptionExample
&& or andLogical AND${(5 > 2) && (3 < 4)} or ${(5 gt 2) and (3 lt 4)}true
`oror`
! or notLogical NOT${!(5 > 6)} or ${not (5 gt 6)}true

🔵 4. Empty Operator

OperatorDescriptionExample
emptyChecks if a value is null, empty string, empty collection, or empty array${empty userList}

✅ If userList is null or empty, empty userList returns true.

Very useful for checking nulls safely!


🔵 5. Conditional Operator (Ternary)

Just like Java’s ? : operator.

SyntaxExample
condition ? valueIfTrue : valueIfFalse${(age > 18) ? 'Adult' : 'Minor'}

✅ Simple way to make decisions inside EL.


🎯 Final Full List:

Operator TypeExamples
Arithmetic+, -, *, / (div), % (mod)
Relational (Comparison)==, !=, <, >, <=, >= (eq, ne, lt, gt, le, ge)
Logical&&, `
Empty Testempty
Conditional (Ternary)condition ? value1 : value2

📢 Important Notes:

  • EL is null-safe. If you reference something that doesn’t exist, it won’t throw an exception — it simply treats it as empty or null.
  • Word operators (eq, lt, and, etc.) are mainly there to be XML-compliant (some HTML parsers break on < or > symbols).

🚀 Super Quick Example Mixing Them:

<c:if test="${not empty user and user.age gt 18}">
    <p>Welcome, ${user.name}! You are an adult.</p>
</c:if>

Checks if user exists and is older than 18.

Greets the user if conditions are met.

This entry was posted in Без рубрики. Bookmark the permalink.