EL (Expression Language) 是 JSP 2.0 核心特性,用於替代 `<%= %>` 腳本,實現頁面與 Java 程式碼分離。
${expression} // 例:${user.name}、${list[0]}、${map.key}、${param.id}
and, eq, gt, true, instanceof, or, ne, le, false, empty, not, lt, ge, null, div, mod
${identifier} 相當於 pageContext.findAttribute("identifier"),依序搜尋:
找不到回傳空字串 ""(非 null),避免 NPE。
| 類型 | 語法範例 | 說明 |
|---|---|---|
| JavaBean | ${user.name}、${user.address.city} | 自動呼叫 getter |
| List/陣列 | ${list[0]}、${list[3].name} | 索引存取 |
| Map | ${map.key}、${map['key-name']} | 鍵值存取,含特殊字元用 [] |
<c:forEach var="user" items="${userList}">
${user.name} - ${user.email}<br/>
</c:forEach>
<c:forEach var="entry" items="${userMap}">
${entry.key} = ${entry.value.name}<br/>
</c:forEach>
| 類別 | 運算符 | 範例 |
|---|---|---|
| 算術 | +, -, *, / (div), % (mod) | ${price * quantity} |
| 關係 | ==, !=, <, >, <=, >= (eq, ne, lt, gt, le, ge) | ${user.age ge 18} |
| 邏輯 | &&, ||, ! (and, or, not) | ${not empty list} |
| empty | 檢查 null、空字串、空陣列、空集合、空 Map | ${empty param.name} |
| 三元 | condition ? value1 : value2 | ${user.loggedIn ? user.name : '訪客'} |
| 存取 | . (屬性)、[] (索引/鍵) | ${list[0]}、${map['key']} |
| 對象 | 型別 | 用途 | 範例 |
|---|---|---|---|
| pageContext | PageContext | 存取 request、session、application 等 | ${pageContext.request.contextPath} |
| pageScope | Map | page 域屬性 | ${pageScope.attr} |
| requestScope | Map | request 域屬性 | ${requestScope.formData} |
| sessionScope | Map | session 域屬性 | ${sessionScope.user} |
| applicationScope | Map | application 域屬性 | ${applicationScope.counter} |
| param | Map<String,String> | 請求參數(單值) | ${param.id} |
| paramValues | Map<String,String[]> | 請求參數(多值) | ${paramValues.hobby[0]} |
| header | Map<String,String> | 請求頭(單值) | ${header['User-Agent']} |
| headerValues | Map<String,String[]> | 請求頭(多值) | ${headerValues.Accept[0]} |
| cookie | Map<String,Cookie> | Cookie 物件 | ${cookie.JSESSIONID.value} |
| initParam | Map<String,String> | web.xml context-param | ${initParam.appName} |
<form action="${pageContext.request.contextPath}/register" method="post">
<input name="username" value="${param.username}" />
<input type="submit" value="註冊" />
</form>
package com.example.util;
public class HtmlUtils {
public static String escape(String input) {
if (input == null) return "";
return input.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
}
}
/WEB-INF/tlds/functions.tld<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="https://java.sun.com/xml/ns/javaee"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>Custom Functions</short-name>
<uri>https://example.com/functions</uri>
<function>
<name>escapeHtml</name>
<function-class>com.example.util.HtmlUtils</function-class>
<function-signature>java.lang.String escape(java.lang.String)</function-signature>
</function>
</taglib>
<%@ taglib prefix="fn" uri="https://example.com/functions" %>
${fn:escapeHtml("")}
// 輸出:<script>alert('XSS')</script>
web.xml version="2.5" 以上,或 JSP 頁面添加 `<%@ page isELIgnored="false" %>`"" 而非 null,搭配 empty 運算符判斷