EL 表达式
EL 表达式的作用就是帮助我们简化 JSP 中的 Java 代码。
示例
1、在四大作用域中取值:
<!-- page.jsp -->
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    pageContext.setAttribute("str", "from pageContext");
    request.setAttribute("str", "from request");
    session.setAttribute("str", "from session");
    application.setAttribute("str", "from application");
%>
${pageScope.str} <br>
${requestScope.str} <br>
${sessionScope.str} <br>
${applicationScope.str} <br>
<%--在 el 表达式中不指定 scope 直接使用 name,会依次在 pageContext->request->session->application --%>
${str}
</body>
</html>

2、取 array  和 list 中的值:
<!-- page.jsp -->
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String[] array = {"a", "b", "c", "d"};
    pageContext.setAttribute("array", array);
    List list = new ArrayList();
    list.add(11);
    list.add(22);
    list.add(33);
    list.add(44);
    pageContext.setAttribute("list", list);
%>
${array[0]},${array[1]},${array[2]},${array[3]}
<hr/>
${list[0]},${list[1]},${list[2]},${list[3]}
</body>
</html>

3、取 map 中的值:
<!-- page.jsp -->
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String[] array = {"aa", "bb"};
    Map map = new HashMap();
    map.put("name1", "zhangsan");
    map.put("name2", "lisi");
    map.put("name3", "wangwu");
    map.put("name4", "zhaoliu");
    pageContext.setAttribute("map", map);
%>
${map["name1"]},${map["name2"]},${map.name3},${map.name4}
</body>
</html>

4、取对象属性的值:
// com.zze.bean.User
package com.zze.bean;
public class User {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
<!-- page.jsp -->
<%@ page import="com.zze.bean.User" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    User user = new User();
    user.setName("张三");
    user.setAge(18);
    pageContext.setAttribute("user", user);
%>
姓名:${pageScope.user.name} <br>
年龄:${user.age}
</body>
</html>

11 大隐式对象
隐式对象即在 EL 表达式中能直接使用的对象,有 11 个如下:
- pageContext:就是 JSP 内置对象的 pageContext。
 - pageScope:当前 JSP 页的 Map 域。
 - requestScope:当前请求范围的 Map 域。
 - sessionScope:当前会话范围的 Map 域。
 - applicationScope:当前应用程序的 Map 域。
 
上面所说的 Map 域指的是使用 setAttribute 存放值存放到的 Map 实例。
- header:请求头键和值的 Map。
 - headerValues:请求头键和值的 Map,值为数组结构。
 - param:请求参数的参数名和值的 Map。
 - paramValues:请求参数的参数名名和值的 Map,值为数组结构。
 - initParam:所有初始化参数的 Map(这里的初始化参数指的是全局初始化参数)。
 - cookie:所有 Cookie 的键和值 Map。
 
      
      
      
评论区