一、el是什么
EL表达式的全称是Expression Languege,是表达式语言.
二、el的作用 <========>代替jsp的表达式脚本<%=%>
EL表达式主要是为了代替jsp页面中的表达式脚本在页面上输出数据.
因为EL表达式的输出要比jsp页面表达式脚本输出代码更加简洁.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<% request.setAttribute("key","value");%>
Jsp表达式脚本输出:<%=request.getAttribute("key")==null?"":request.getAttribute("key")%><br/>
El表达式输出:${key}<br/><br/><br/>
<%--
EL表达式搜索域数据的顺序
pageContext ==>> request ==>> session ==>> application
--%>
<%
pageContext.setAttribute("pageKey","pageValue");
request.setAttribute("reqKey","reqValue");
session.setAttribute("sesKey","sesValue");
application.setAttribute("appKey","appValue");
%>
${pageKey}<br/>
${reqKey}<br/>
${sesKey}<br/>
${appKey}<br/><br/>
<%
pageContext.setAttribute("key","pageValue");
request.setAttribute("key","reqValue");
session.setAttribute("key","sesValue");
application.setAttribute("key","appValue");
%>
${key}<br/><br/><br/>
</body>
</html>
三、el表达式语法格式
EL表达式的语法格式是: ${ 表达式 }
EL表达式在输出空值的时候,输出的是空串.
jsp的表达式脚本在输出空值的时候,输出的是null字符串.
EL表达式主要是用来输出域对象中的数据.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Person person = new Person();
person.setId(100);
person.setPhones(new String[]{"18610541354","18666668888","13988889999"});
List<String> cities = new ArrayList<>();
cities.add("北京");
cities.add("上海");
cities.add("深圳");
person.setCities(cities);
Map<String,Object> map = new HashMap<>();
map.put("aaaa", "aaaaValue");
map.put("bbbb", "bbbbValue");
map.put("cccc", "ccccValue");
person.setMap(map);
request.setAttribute("p", person);
%>
输出person对象: ${p} <br>
输出id属性的值:${p.id} <br>
输出phones属性的值:${p.phones[0]} <br>
输出phones属性的值:${p.phones[1]} <br>
输出phones属性的值:${p.phones[2]} <br><br><br>
输出 list 集合的值:${p.cities} <br>
输出 list 元素的值:${p.cities[0]} <br>
输出 list 元素的值:${p.cities[1]} <br>
输出 list 元素的值:${p.cities[2]} <br><br><br>
输出 map 集合的值:${p.map} <br>
输出 map 某个key的值:${p.map.aaaa} <br>
输出 map 某个key的值:${p.map.bbbb} <br>
输出 map 某个key的值:${p.map.cccc} <br><br><br>
<%--输出 age 属性的值: ${p.age}--%> <br>
</body>
</html>
四、el表达式搜索域数据的顺序
EL在搜索域数据的时候,它搜索的四个域的顺序分别是从小到大.
pageContext ==>> request ==>> session ==>> application
五、el表达式运算
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %><%--
Created by IntelliJ IDEA.
User: 13071
Date: 2020/8/3
Time: 19:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el</title>
</head>
<body>
<%--关系运算符--%>
<%--等于--%>
${5==5} ${5 eq 5}<br>
<%--不等于--%>
${5!=5} ${5 ne 5}<br>
<%--小于--%>
${3 < 5} ${3 lt 5}<br>
<%--大于--%>
${3 > 5} ${3 gt 5}<br>
<%--小于等于--%>
${ 5 <= 12 } ${ 5 le 12 }<br>
<%--大于等于--%>
${ 5 >= 12 } ${ 5 ge 12 }<br><br><br>
<%--逻辑运算符--%>
<%--&& 或 and 与运算:有一个false,则结果为false--%>
${ 12 == 12 && 12 < 11 } ${12 ==12 and 12 <11}<br>
<%--|| 或 or 或运算:有一个为true,则结果为true--%>
${ 12 == 12 || 12 < 11 } ${ 12 == 12 or 12 < 11 }<br>
<%--! 或 not 取反运算--%>
${ !true } ${not false }<br>
<%--算数运算--%>
${12 + 8} ${12 - 8} ${12 * 8} ${12 / 8} 或${12 div 8}<br>
${20 % 6} ${20 mod 7}<br><br><br>
<%--
empty
empty运算可以判断某个数据是否为空.如果为空输出true.不空输出false.
以下几种情况下.会为空,也就是会输出true.
1 值为null的情况.
2 值为空串
3 值是Object类型的数组,且长度为零
4 list集合,元素个数为零
5 map集合,元素个数为零
--%>
<%--
表达式1 ? 表达式2 : 表达式3
如果表达式1的值为true,返回表达式2的值.
如果表达式1的值为false.返回表达式3的值
--%>
<%
request.setAttribute("emptyNull",null);
request.setAttribute("emptyStr","");
request.setAttribute("emptyArr",new Object[]{});
List<String> list = new ArrayList<>();
list.add("张三");
request.setAttribute("emptyList",list);
request.setAttribute("emptyMap",new HashMap<>());
%>
${empty emptyNull}<br>
${empty emptyStr?"Y":"N"}<br> <%--三目运算符--%>
${empty emptyArr}<br>
${empty emptyList}<br>
${empty emptyMap}<br><br><br>
<%--
ii.“.”点运算 和 [] 中括号运算符
“.”点运算 可以输出bean的属性值.也可以输出map集合中某个key的值.
[] 中括号运算符 可以输出有序集合中指定元素的值. 还可以输出map中key里含有特殊字符的key的值
--%>
<%
Map<String ,Object> map = new HashMap<>();
map.put("key1", "aaaValue");
map.put("key2", "bbbValue");
map.put("key3", "cccValue");
request.setAttribute("map",map);
Map<String ,Object> map1 = new HashMap<>();
map1.put("a.a.a", "aaaValue");
map1.put("b+b+b", "bbbValue");
map1.put("c-c-c", "cccValue");
request.setAttribute("map1",map1);
%>
${map}
${map[key1]}<br>
${map[key2]}<br>
${map[key3]}<br><br><br>
<%--${map.entrySet()}<br>--%>
${map1}
${map1[a.a.a]}<br>
${map1[b+b+b]}<br>
${map1[c-c-c]}<br>
</body>
</html>
六、EL表达式的11个隐含对象
变量名 数据类型 作用描述
pageContext PageContextImpl 在EL表达式中获取jsp中的九大内置对象
pageScope Map<String,Object> 获取pageContext域中的数据
requestScope Map<String,Object> 获取request域中的数据
sessionScope Map<String,Object> 获取Session域中的数据
applicationScope Map<String,Object> 获取ServletContext域中的数据
param Map<String,String> 获取请求的参数值
paramValues Map<String,String[]> 获取请求的参数值(获取多个值)
header Map<String,String> 获取请求头的值
headerValues Map<String,String[]> 获取请求头的值(多个值的情况)
cookie Map<String,Cookie> 获取Cookie信息
initParam Map<String,String> 获取web.xml中配置的Context-param参数
(1)EL获取四个特定域中的属性
pageScope <<>> pageContext域
requestScope <<>> request域
sessionScope <<>> session域
applicationScope <<>> ServletContext域
(2)pageContext作用:获取jsp中的九大内置对象.–见el04.jsp
(3)param/paramValues隐含对象:获取请求参数信息(el常用隐含对象)
(4)header/headerValues隐含对象:获取请求头信息(el常用隐含对象)
(5)cookie/initParam隐含对象:获取cookie信息/初始化参数(配置在web.xml)
<%--
Created by IntelliJ IDEA.
User: 13071
Date: 2020/8/3
Time: 21:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
pageContext作用:获取jsp中的九大内置对象.
然后调用内置对象的方法,从而获取需要的信息
1.协议:
2.服务器ip:
3.服务器端口:
4.获取工程路径:
5.获取请求方法:
6.获取客户端ip地址:
7.获取会话的id编号:
--%>
<%
pageContext.setAttribute("req",request);
%>
①:获取请求协议:${req.scheme}<br>
②:获取服务器ip:${req.serverName}<br>
③:获取服务器端口:${req.serverPort}<br>
④:获取工程路径:${req.contextPath}<br>
⑤:获取请求方法:${req.method}<br>
⑥:获取客户端ip地址:${req.remoteHost}<br>
⑦:获取会话id编号:${req.session.id}<br><br><br>
①:${pageContext.request}<br>
②:${pageContext.request.scheme}<br>
③:${pageContext.request.serverName}<br>
④:${pageContext.request.serverPort}<br>
⑤:${pageContext.request.contextPath}<br>
⑥:${pageContext.request.method}<br>
⑦:${pageContext.request.remoteHost}<br>
⑧:${pageContext.request.session.id}<br>
⑨:${pageContext.request.cookies}<br>
⑩:${pageContext.request.getHeader("User-Agent")}<br>
<%
ServletContext servletContext = pageContext.getRequest().getServletContext();
// java.lang.IllegalStateException: Initialization parameters can not be set after the context has been initialized
// 初始化参数可以在配置web.xml时进行定义,在servletContext对象加载之后,是不可以在通过以下
// servletContext.setInitParameter("username","cs01");
// servletContext.setInitParameter("password","123");
// 方式进行初始化参数定义的,因为servletContext对象随着容器的启动而创建
servletContext.setAttribute("username","cs01");
servletContext.setAttribute("password","123");
pageContext.setAttribute("servletContext",servletContext);
%>
${servletContext.contextPath}<br>
${servletContext.getRealPath("")}<br>
<%--
${servletContext.getInitParameter("username")}<br>
${servletContext.getInitParameter("password")}<br>
--%>
获取servletContext域对象中设置的username值:${servletContext.getAttribute("username")}<br>
获取servletContext域对象中设置的password值:${servletContext.getAttribute("password")}<br><br><br>
<%--param/paramValues隐含对象:获取请求参数信息--%>
<%--
param Map<String,String> 获取请求的参数值
paramValues Map<String,String[]> 获取请求的参数值(获取多个值)
--%>
获取请求参数username:${param.username}<br>
获取请求参数password:${param.password}<br>
获取请求参数hobby:${paramValues.hobby[0]}<br>
获取请求参数hobby:${paramValues.hobby[1]}<br><br><br>
<%--header/headerValues隐含对象:获取请求头信息--%>
<%--
header Map<String,String> 获取请求头的值
headerValues Map<String,String[]> 获取请求头的值(多个值的情况)
--%>
获取请求头host的值:${header.host}<br>
获取请求头 user-Agent 的值: ${ header['user-agent'] } <br>
获取请求头 Accept 的值: ${ header.accept } <br>
获取请求头 host 的值,值headerValues : ${ headerValues.host[0] }<br><br><br><br>
<%----%>
<%--
cookie Map<String,Cookie> 获取Cookie信息
initParam Map<String,String> 获取web.xml中配置的Context-param参数
--%>
获取cookie中name的值:${cookie.JSESSIONID.name}<br>
获取cookie中value的值:${cookie.JSESSIONID.value}<br><br>
获取context-param username 的值:${initParam.username}<br>
获取context-param url 的值:${initParam.url}<br>
获取context-param password 的值:${initParam.password}<br>
</body>
</html>