Java.Servlet.What do you know about JSP Expression Language (EL)?

JSP Expression Language (EL) is a simple and powerful way to access Java objects in JSP pages without writing Java code (no <% %> needed).

  • It allows you to access request attributes, session attributes, application attributes, JavaBeans properties, collections, and even call simple functions.
  • Introduced in JSP 2.0 to replace scriptlets for reading data.

✅ It makes your JSP pages cleaner, easier to maintain, and more readable.

🔥 Syntax of EL

${expression}

${} is used to evaluate the expression at runtime.

Example:

<p>Hello, ${user.name}!</p>

Here, user is an attribute (probably stored in request/session), and .name accesses its getName() property.

🔵 Key Features of EL

FeatureDescription
Simplified accessAccess objects easily without Java code
Automatic scope searchEL searches page, request, session, application scopes automatically
Bean property accessCall getters automatically (user.nameuser.getName())
Collection accessAccess arrays, lists, maps using [ ] syntax
Operators supportLogical, arithmetic, relational operators
Functions supportCall custom or standard functions via JSTL

🛠️ Examples of EL Usage

1. Accessing Bean Properties

${user.name}
${user.age}

Will call getName() and getAge() methods of user bean.

Accessing Maps/Lists/Arrays

${productList[0].name}
${userMap["admin"].email}

Access elements inside lists and maps.

Using Logical/Relational/Arithmetic Operators

${age > 18}
${price * quantity}
${empty shoppingCart}

EL supports operators like +, -, *, /, %, >, <, ==, !=, and, or, empty.

4. Scope-Specific Access

You can force EL to look in a particular scope:

${sessionScope.user}
${requestScope.order}
${applicationScope.globalSetting}
${pageScope.tempData}

Normally, EL automatically searches in this order:

  1. page
  2. request
  3. session
  4. application

5. Handling Null Values

If a property or variable does not exist:

  • EL will return an empty string "" (not throw a NullPointerException).

Example:

<p>${nonExistentVar}</p> <!-- Will output nothing -->

⚡ Why Use EL?

ReasonExplanation
No Java code in JSPCleaner, more readable code
Separation of concernsJSP focuses on display only
Automatic handlingNulls, missing properties are handled gracefully
Supports JSTLWorks beautifully with JSTL tags (<c:if>, <c:forEach>, etc.)

🎯 Final Short Summary:

TermMeaning
ELA powerful language for accessing server-side objects from JSP
Syntax${expression}
GoalMake JSP pages clean, logic-free, and purely for presentation

📢 One more important point:

  • You cannot write complicated Java logic inside EL (like loops, file operations, etc.).
  • EL is only for simple evaluations — business logic must stay in Java classes, services, or controllers!
This entry was posted in Без рубрики. Bookmark the permalink.