JSP Expression Language (EL) – Lecture Notes
Expression Language (EL) in JSP is used to simplify the access to data stored in JavaBeans
components, request/session/application attributes, and more without using Java code in
the JSP page.
1. Syntax:
Basic format: ${variableName}
2. Why Use EL?
- Shorter and cleaner syntax than scriptlets
- Easy access to attributes in different scopes
- Improves code readability and maintainability
3. Scope Resolution Order:
When you write ${name}, EL searches for 'name' in the following scopes:
1. Page Scope
2. Request Scope
3. Session Scope
4. Application Scope
4. Example of Setting and Using EL:
// In Servlet
request.setAttribute("username", "May Zin Htun");
// In JSP
Welcome, ${username}!
5. Using Operators in EL:
Arithmetic: ${mark + 10}, ${price * quantity}
Relational: ${mark >= 40}, ${age < 18}
Logical: ${passed && excellent}, ${!failed}
6. Common EL Objects:
param: ${param.name} (Form parameter)
paramValues: ${paramValues.name} (Multiple form values)
header: ${header['User-Agent']} (HTTP headers)
cookie: ${cookie.sessionID.value} (Cookies)
initParam: ${initParam.configName} (Context params)
7. EL Functions (with JSTL):
Example: ${fn:length(name)} returns length of the name string
Expression Language helps you write cleaner, simpler JSP pages. Practice using EL in your
form handling and dynamic display pages. ❤️