EL表达式
一、EL表达式介绍二、EL从四大域中获取属性以及搜索顺序三、EL表达式中的运算1.关系运算2.逻辑运算3.Empty运算符4.[] 运算符
EL表达式中的11个内置对象1.从四个域中获取特定属性2.pageContext的使用3.其他隐含对象
一、EL表达式介绍
是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据)
${expression}
二、EL从四大域中获取属性以及搜索顺序
request:类型:HttpServletRequest;表示当前的请求。
session:类型:HttpSession;表示一次会话。 与用户一一对应。
application:类型:ServletContext;表示应用程序本身。实现多用户数据共享。
pageContext:类型:PageContext;表示页面上下文,可以用于定义当前页面中需要使用的变量。
特殊:该对象还可以获得其它8个内置对象。
pageContext.setAttribute("key", "pageContext");
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
<body>
<%
pageContext.setAttribute("key", "pageContext");
request.setAttribute("key", "request");
session.setAttribute("key", "session");
application.setAttribute("key", "application");
%>
${key }
</body>
结果:pageContext
根据key检索顺序(由小到大):pageContext->request->session->application
三、EL表达式中的运算
1.关系运算
关系运算符范例结果
==或eq
5
=
=
5
或
{5==5}或
5==5或{5eq5}true!=或ne
5
!
=
5
或
{5!=5}或
5!=5或{5ne5}false<或lt
3
<
5
或
{3<5}或
3<5或{3lt5}true>或gt${3>5}或{3gt5}false<=或le
3
<
=
5
或
{3<=5}或
3<=5或{3le5}true>=或ge5}或${3ge5}false
2.逻辑运算
关系运算符范例结果
&&或andKaTeX parse error: Expected '}', got '&' at position 4: {A &̲& B}或{A and B}true/false||或or
A
∥
B
或
{A \|\ B}或
A∥ B或{A or B}true/false!或not
!
A
或
{! A }或
!A或{not A}true/false
3.Empty运算符
${! empty key}
${ empty key}
1若key为null时,返回true
2若key为空String时,返回true
3若key为空Array时,返回true
4若key为空Map时,返回true
5若key为空Collection时,返回true
6否则,返回false
4.[] 运算符
[] 运算符可以输出有序集合中某个元素的值。
<body>
<%
Map
<String,Object> map = new HashMap
<String,Object>();
map.put("a.a", "aa");
map.put("a+aa", "aaa");
map.put("aa-aa", "aaaa");
request.setAttribute("map", map);
%>
${ map["a.a"] }
<br>
${ map['a+aa'] }
</body>
结果:
aa
aaa
EL表达式中的11个内置对象
pageScope:获取pageContext域属性,相当于pageContext.getAttribute("xxx")
requestScope:获取request域属性,相当于request.getAttribute("xxx")
sessionScope:获取session域属性,相当于session.getAttribute("xxx")
applicationScope:获取application域属性,相当于application.getAttribute("xxx")
param:对应参数,它是一个Map,其中key是参数,value是参数值,适用于单值的参数,相当于request.getParameter("xxx")
paramValues:对应参数,她是一个Map,其中key是参数,value是多个参数值,适用于多值的参数,相当于request.getParameterValues("xxx")
header:对应请求头,它是一个Map,其中key表示头名称,value是单个头值,适用于单值的请求头,相当于request.getHeader("xxx")
headerValues:对应请求头,它是一个Map,其中key表示头名称,value是多个头值,适用于多值的请求头,相当于request.getHeaders("xxx")
initParam:获取web.xml中
<context-param>内的参数,${ initParam.xxx},xxx就是
<param-name>标签内的值,进而得到
<param-value>中的值
cookie:用于获取cookie,Map
<String,Cookie>,其中key是cookie的name,value是cookie对象,例如${cookie.JSESSIONID.value }就是获取sessionId
pageContext:可以获取JSP九大内置对象,相当于使用该对象调用getxxx()方法,例如pageContext.getRequest()可以写为${pageContext.request)
1.从四个域中获取特定属性
EL获得pageContext域中的值:${pageContextScope.key};
EL获得request域中的值:${requestScope.key};
EL获得session域中的值:${sessionScope.key};
EL获得application域中的值:${applicationScope.key};
2.pageContext的使用
<body>
<%--
request.getScheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器ip或域名
request.getServerPort() 获取请求的服务器端口号
getContextPath() 获取当前工程路径
request.getMethod() 获取请求的方式(GET或POST)
request.getRemoteHost() 获取客户端的ip 地址
session.getId() 获取会话的唯一标识
--%>
<%
pageContext.setAttribute("req", request);
%>
<%=request.getScheme() %>
<br>
1.协议: ${ req.scheme }
<br>
2.服务器ip:${ pageContext.request.serverName }
<br>
3.服务器端口:${ pageContext.request.serverPort }
<br>
4.获取工程路径:${ pageContext.request.contextPath }
<br>
5.获取请求方法:${ pageContext.request.method }
<br>
6.获取客户端ip地址:${ pageContext.request.remoteHost }
<br>
7.获取会话的id编号:${ pageContext.session.id }
<br>
</body>
结果:
http
1.协议: http
2.服务器ip:localhost
3.服务器端口:8080
4.获取工程路径:/first
5.获取请求方法:GET
6.获取客户端ip地址:0:0:0:0:0:0:0:1
7.获取会话的id编号:CE8240C1A5B29C1271D0D9A1EAB3A2DA
3.其他隐含对象
传入数据:username=wzg168&password=666666&hobby=java&hobby=cpp
<body>
输出请求参数username的值:${ param.username }
<br>
输出请求参数password的值:${ param.password }
<br>
输出请求参数username的值:${ paramValues.username[0] }
<br>
输出请求参数hobby的值:${ paramValues.hobby[0] }
<br>
输出请求参数hobby的值:${ paramValues.hobby[1] }
<br>
<hr>
输出请求头【User-Agent】的值:${ header['User-Agent'] }
<br>
输出请求头【Connection】的值:${ header.Connection }
<br>
输出请求头【User-Agent】的值:${ headerValues['User-Agent'][0] }
<br>
<hr>
获取Cookie的名称:${ cookie.JSESSIONID.name }
<br>
获取Cookie的值:${ cookie.JSESSIONID.value }
<br>
<hr>
输出
<Context-param
>username的值:${ initParam.username }
<br>
输出
<Context-param
>url的值:${ initParam.url }
<br>
</body>
结果:
输出请求参数username的值:wzg168
输出请求参数password的值:666666
输出请求参数username的值:wzg168
输出请求参数hobby的值:java
输出请求参数hobby的值:cpp
输出请求头【User-Agent】的值:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
输出请求头【Connection】的值:keep-alive
输出请求头【User-Agent】的值:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
获取Cookie的名称:JSESSIONID
获取Cookie的值:CE8240C1A5B29C1271D0D9A1EAB3A2DA
输出
<Context-param>username的值:
输出
<Context-param>url的值: