目录
EL表达式: Expression Language
1)方便获取数据的语言
${EL表达式语言}
2)EL的隐式对象:
与范围有关的隐式对象:
- pageScope
- requestScope
- sessionScope
- applicationScope
数值类型
${100+200 } ${100/5 } ${100%3 } ${'10'+'20'}
布尔类型
${10>20}
字符串
${"abc" }/${'abc'}
判断某个值是否是空值
${empty '' }
${empty 'abc' }
${empty null }
${empty a }
关系表达式
【==】 【!=】 【>=】 【<=】
${10!=20 } ${30 >=20 }
值返回真true或者假false
逻辑运算符
&& || !
${5>4 && 3<1 }
el表达式与隐式对象有关的
<%
pageContext.setAttribute("user", "容儿"); //pageScope
request.setAttribute("user", "祖儿"); //requestScope
session.setAttribute("user", "小青"); //sessionScope
application.setAttribute("user", "小白"); //applicationScope
%>
${pageScope.user} <!-- 可以写成 &{user} -->
${requestScope.user }
${sessionScope.user }
${applicationScope.user}
与请求有关的el表达式
${param.id}//相当于,直接使用传输的隐式对象
<%=request.getParmeter("id")%>
//获取数值
${paramValues.loves}
//获取数组中第一个值
${paramValues.loves[0]}
获取头信息
//获取头信息
${header['host']}
//获取头信息的所有值,返回是一个数组
${headerValue}
获取上下文路径
//获取上下文路径,这个对象可以拿到其它8个隐式对象
${pageContext.request.contextPaht}
获取全局的初始化参数信息
配置全局参数信息
<context-param>
<param-name>aaaa</param-name>
<param-value>myvalue<param-value>
</context-oaram>
获取全局的参数配置信息
方法1:
<%=application.getInitParameter("aaaa")%>
方法2:
${initParam.aaaa}
获取cookie中的数据
//cookie中存储数据
<%
Cookie c=new Cookie("user","1234"):
c.setMaxAge(60*60);
c.setPath(reqyest.getContextPath);
response.addCookie(c);
%>
//方法1:cookie取数据
<%
Cookie cookies[]=request.getCookie();
for(Cookie cookie:cookies){
out.print(cookie.getName()+"---"+cookie.getValue());
}
%>
//方法2:用el表达式获取cookie的值
${cookie.user.name} //获取cookie中的键
${cookie.user.value} //获取cookie中的值
练习:el练习
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>原样输出</h1>
100+20 100/5 100%3 10'+'20'
<hr>
<hr>
<h1>数值类型</h1>
${100+200 } ${100/5 } ${100%3 } ${'10'+'20'}
<%-- ${'容'+'儿'} --%>
<hr>
<h1>布尔类型</h1>
${10>20}
<hr>
<h1>字符串</h1>
${"abc" }/${'abc'}
<h1>判断某个值是否是空值</h1>
${empty '' }/${empty 'abc' }/${empty null }/${empty a }
<hr>
<h1>关系表达式</h1>
== != <(lt) >(gt) >=(ge) <= (le)
<br> ${10!=20 } ${30 >=20 }
<hr>
<h1>逻辑运算符</h1>
&&/and ||/or !/not
<br> ${5>4 && 3<1 }
<hr>
<h1>el表达式与隐式对象有关的</h1>
<%
pageContext.setAttribute("user", "容儿"); //pageScope
request.setAttribute("user", "祖儿"); //requestScope
session.setAttribute("user", "小青"); //sessionScope
application.setAttribute("user", "小白"); //applicationScope
%>
${pageScope.user} <!-- 可以写成user -->
${requestScope.user }
${sessionScope.user }
${applicationScope.user}
</body>
</html>