一、EL表达式介绍
Expression Language表达式语言
是一种在JSP页面获取数据的简单方式(只能获取数据,不能设置数据)
在JSP2.0开始引入概念
语法格式
在JSP页面的任何静态部分均可通过:${expression}来获取到指定表达式的值
二、EL获取数据(从四大域中获取属性)
EL只能从四大域中获取属性
1、如果没有使用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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${name }
</body>
</html>
没有使用EL内置对象时查找顺序是由小到大,所以最先显示的是pageContext域中的
2、使用EL内置对象,从指定域中获取数据,提高了查找效率
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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }
</body>
</html>
使用applicationScope即可指定application域中的name输出,当然其他域也是类似,下文会说这四大域属性相关的内置对象
三、EL中的内置对象
EL有11个内置对象,这里主要讲域属性相关的4个和其他4个
EL的11个内置对象,除了pageContext以外,其他10个内置对象的类型都是java.util.Map类型
1、域属性相关(4个)
1、pageScope:从page范围域属性空间中查找指定的key
2、requestScope:从request范围域属性空间中查找指定的key
3、sessionScope:从session范围域属性空间中查找指定的key
4、applicationScope:从application范围域属性空间中查找指定的key
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>
<%
pageContext.setAttribute("name", "linjie");
request.setAttribute("name", "lucy");
session.setAttribute("name", "king");
application.setAttribute("name", "bilibili");
%>
name=${applicationScope.name }<br>
name=${pageScope.name }<br>
name=${sessionScope.name }<br>
name=${requestScope.name }<br>
</body>
</html>