Nearby lessons

14 of 30

JSP - EL Operators

📌 What You Will Learn
  • Use property access (.) and collection access ([]) operators
  • Master arithmetic, relational, and logical operators
  • Understand the empty operator and EL null handling

EL Operators provide arithmetic, relational, logical, and conditional operations directly in EL expressions. This lesson covers all EL operators, the empty operator, and operator precedence.

1) Property Access Operator (.)

Syntax: ${left.right}

  • Left side: a Map or a Bean
  • Right side: a Map key or Bean property (must be a valid Java identifier)
Example01
JCode Cell
1 
2${customer.name} → bean property
3${pageContext.request.method} → chained access
4${initParam.user} → context parameter
5${customer.1234} → INVALID (not a valid identifier)
6

2) Collection Access Operator ([])

Syntax: ${left[right]}

Works with Maps, Beans, Arrays, and Lists.

Left TypeRight TypeQuotes Required?
MapKeyYes — must use single or double quotes
BeanPropertyYes
ArrayIndexNo — quotes optional
ListIndexNo — quotes optional
Example02
JCode Cell
1 
2${initParam["user"]} → map key with quotes (valid)
3${initParam['user']} → map key with quotes (valid)
4${initParam[user]} → blank! (no quotes = attribute lookup)
5${s[0]} → array index (no quotes needed)
6${list[index]} → variable as index
7

3) Arithmetic Operators

OperatorSymbolKeywordExampleResult
Addition+${2+3}5
Subtraction-${10-3}7
Multiplication*${10*3}30
Division/div${10/2}5.0 (always floating-point)
Modulus%mod${10%3}1

Arithmetic Rules

  • No string concatenation with + — it always means addition. EL does not support operator overloading.
  • Null is treated as 0 in arithmetic: ${10+null} = 10
  • Empty string is treated as 0: ${""+3} = 3
  • Non-numeric strings cause NumberFormatException: ${"abc"+3} = RE
  • Undefined attributes are treated as 0: ${abc+3} = 3
  • Division always follows floating-point arithmetic: ${10/2} = 5.0, ${10/0} = Infinity
  • Modulus follows integral arithmetic: ${10%0} = ArithmeticException

4) Relational Operators

OperatorSymbolKeywordExampleResult
Greater than>gt${10>3}true
Less than<lt${10<3}false
Equal==eq${abcd==abc}true
Greater or equal>=ge${10>=10}true
Less or equal<=le${5<=3}false
Not equal!=ne${10!=3}true

5) Logical Operators

OperatorSymbolsKeywordExample
AND&, &&and${true && false} → false
OR|, ||or${true || false} → true
NOT!not${!true} → false

6) Conditional Operator

Syntax: ${condition ? trueValue : falseValue}

Example07
JCode Cell
1 
2${(3<4) ? "yes" : "no"} → yes
3

7) Empty Operator

Syntax: ${empty object}

Returns true if the object:

  • Does not exist (is null)
  • Is an empty string
  • Is an empty array or collection

Returns false otherwise.

Example08
JCode Cell
1 
2${empty durga} → true (variable doesn't exist)
3${empty "durga"} → false (non-empty string)
4${empty null} → true
5${empty ""} → true (empty string)
6

EL Operator Precedence

From highest to lowest:

  1. Unary: !, empty
  2. Multiplicative: *, /, %
  3. Additive: +, -
  4. Relational: >, <, >=, <=
  5. Equality: ==, !=
  6. Logical AND: &&, and
  7. Logical OR: ||, or
  8. Conditional: ? :

EL Reserved Words

These words cannot be used as identifiers in EL:

true · false · null · empty · instanceof · gt · lt · ge · le · ne · eq · and · or · mod · div · not

EL vs Null

EL handles null very gracefully:

  • Arithmetic: null is treated as zero
  • Strings: null is treated as empty string
  • Logical: null is treated as false
Example11
JCode Cell
1 
2${10+null} → 10
3${empty null} → true
4${!null} → true
5
📝 Key Takeaways
  • . accesses map keys or bean properties; [] accesses map keys, bean properties, or array/list indices
  • + always means addition — no string concatenation in EL
  • empty returns true for null, empty strings, empty arrays, and empty collections

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3