EL表达式的全称是: expression Language。是表达式语言
EL表达式的作用:EL表达式主要作用是代替jsp页面中的表达式在jsp页面中进行数据的输出。
因为EL表达式在输出数据的时候,要比jsp的表达式脚本要简洁很多
EL表达式与jsp脚本的区别EL表达式的格式: ${表达式}
- 当为空值时jsp脚本会输出空值null,而EL表达式什么都不会
<%
request.setAttribute("key","致橡树");
%>
表达式脚本输出key的值是:<%=request.getAttribute("key")%>
EL表达式输出key的值是:${key}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZKg4JVqk-1637898525135)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126073727112.png)]
EL表达式搜索域数据的顺序El表达式主要是在jsp页面中输出数据。
主要是输出域对象中的数据
当四个域中都有相同的key的数据时,EL表达式或按照四个域的从小到大的顺序进行搜索,找到输出
<%
pageContext.setAttribute("key","pageContext");
request.setAttribute("key","request");
session.setAttribute("key","session");
application.setAttribute("key","application");
%>
EL表达式输出key的值是:${key}
EL表达式输出Bean的普通属性,数组属性。List集合属性,map集合属性
<%
Person person=new Person();
person.setName("这世界我不喜欢");
person.setPhones(new String[]{"12345","09876","678909"});
List list=new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("成都");
person.setCities(list);
Map map = new HashMap();
map.put("key1",1);
map.put("key2",2);
map.put("key3",3);
person.setMap(map);
pageContext.setAttribute("p",person);
%>
输出Person:${p}
输出Person中的name属性:${p.name}
输出Person中的phones属性:${p.phones[1]}
输出Person中的cities属性:${p.cities}
输出Person中的List集合属性:${p.cities.get(1)}
输出Person中的Map集合属性:${p.map.get("key1")}
运行结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MXMlROHK-1637898525143)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126081010097.png)]
empty运算 empty运算可以判断一个数据是否为空,如果为空输出true,不为空输出false,
一下几种情况为空
- 值为null的时候
- 值为字符串的时候
- 值是Object类型数组,长度为零的时候
- list集合,元素个数为零
- map集合,元素个数为零
<%
//1. 值为null的时候
request.setAttribute("emptyNull",null);
//2. 值为字符串的时候
request.setAttribute("emptyStr","");
//3. 值是Object类型数组,长度为零的时候
request.setAttribute("emptyArr",new Object[]{});
//4. list集合,元素个数为零
List list=new ArrayList<>();
request.setAttribute("emptyList",list);
//5. map集合,元素个数为零
Map map=new HashMap<>();
request.setAttribute("emptyMap",map);
%>
//1. 值为null的时候
${empty emptyNull}
//2. 值为字符串的时候
${empty emptyStr}
//3. 值是Object类型数组,长度为零的时候
${empty emptyArr}
//4. list集合,元素个数为零
${empty emptyList}
//5. map集合,元素个数为零
${empty emptyMap}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-imD2jbh2-1637898525147)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126092425409.png)]
. 运算和 []运算
<%
Map map=new HashMap<>();
map.put("a.a.a","aaaValue");
map.put("b+b+b","bbbValue");
map.put("c-c-c","cccValue");
request.setAttribute("map",map);
%>
${map['a.a.a']}
${map['b+b+b']}
${map['c-c-c']}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yRx2xrvV-1637898525152)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126093732529.png)]
EL表达式中的11个隐含对象 EL表达式张11个隐含对象,式EL表达式中自己定义,可直接使用
| 变量 | 类型 | 作用 |
|---|---|---|
| pageContext | PageContextImpl | 它可以获取jsp中的九大内置对象 |
| pageScope | Map | 它可以获取pageContext域中的数据 |
| requestScope | Map | 获取Request域中的数据 |
| sessionScope | Map | 获取Session域中的数据 |
| applicationScope | Map | 获取ServletContext域中的数据 |
| param | Map | 获取请求参数的值 |
| paramValues | Map | 获取请求参数值,获取多个参数值的时候使用 |
| header | Map | 获取请求头的信息 |
| headerValues | Map | 获取请求头的信息,它可以获取多个值的情况时使用 |
| cookie | Map | 获取当前请求的cookie信息 |
| initParam | Map | 获取web.xml中标签的上下文参数 |
pageScope =====》pageContext域
requestScope =====》Request域
sessionScope =====》Session域
applicationScope =====》ServletContext域
pageContext对象的使用- 协议
- 服务器ip
- 服务器端口
- 获取工程路径
- 获取请求方法
- 获取客户端IP地址
- 获取会话id编号
<%-- request.getScheme() 可以获取请求的协议 request.getServerName() 获取请求 request.getServerPort() 获取请求服务器端口号 request.getContextPath() 获取当前的工程路径 request.getMethod() 获取请求的方式(GET或POST) request.getRemoteHost() 获取客户端的ip地址 session.getId() 获取对象的唯一标识 --%> <%=request.getScheme()%>param与paramValues 获取请求参数
<%=request.getServerName()%>
<%=request.getServerPort()%>
<%=request.getContextPath()%> <%=request.getMethod()%> <%=request.getRemoteHost()%> <%=session.getId()%> 1. 协议:${pageContext.request.scheme}
2. 服务器ip:${pageContext.request.serverName}
3. 服务器端口:${pageContext.request.serverPort}
4. 获取工程路径:${pageContext.request.contextPath}
5. 获取请求方法:${pageContext.request.method}
6. 获取客户端IP地址:${pageContext.request.remoteHost}
7. 获取会话id编号:${pageContext.session.id}
输出请求参数username的值:${param.username}
输出请求参数password的值:${param.password}
输出请求参数username的值:${paramValues.username[0]}
输出请求参数password的值:${paramValues.password[0]}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QHZ9pRit-1637898525156)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126110957902.png)]
header与headerValues获取请求头的信息
${header}
输出请求头User-Agent的值: ${header['User-Agent']}
输出请求头Connection的值: ${header['Connection']}
输出请求头User-Agent的值:${headerValues["User-Agent"][0]}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EP23V28J-1637898525159)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126113056688.png)]
initParam对象
输出<context-param>-username的值: ${initParam.username}
输出<context-param>-url的值: ${initParam.url}
运行结果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3gjL7VYo-1637898525161)(C:Users24386AppDataRoamingTyporatypora-user-imagesimage-20211126114301281.png)]



