EL内置对象

EL 11 个内置对象
pageContext

pageScope
requestScope
sessoinScope
appliationScope
 
param
paramValues
 
header
headerValues
 
cookie
initParam

通过 EL 内置对象获得数据
1)pageContext:该对象既是El内置对象也是JS内置对象,通常用于获取web应用路径
2)pageScope/requestScope/sessionScope/applicationScrop:获取对应域中的数据
3)param和paramValue:获取请求参数的数据 
4)header和headerValues:获取请求头信息
5)cookie:获取浏览器保存的cookie信息
6)initParam:获取web应用的全局参数

注意:
以上11个el内置对象除了pageContext是非集合对象外,其余对象获取的全是map集合,直接输出的对象结果是{key1=value1,key2=value2,...}.

el内置对象获取属性值时可以用[]也可以用'.';如果属性名中有特殊符号的,只能使用[]方式获取.
例如${pageContext.request.contextPath}
或者${pageContext["request"]["contextPath"]}
pageContext是el内置对象,"pageContext.request"相当于pageContext.getRequest()
request是HttpRequest对象,"request.contextPath"相当于request.getContextPath()

关于cookie内置对象
${cookie} 获取map的结果是{key=Cookie}
例如:创建cookie
Cookie c=new Cookie("username","tom");
通过${cookie}获取的结果是:{username=javax.servlet.http.Cookie@4240287f}
观察可知上述map对象的key等于cookie对象的key,map的value等于当前cookie对象

若想获取名称username的cookie的name值
${cookie.username.name}或者${cookie["username"].name}或者${cookie["username"]["name"]}

若想获取名称username的cookie的value值
${cookie.username.value}或者${cookie["username"].value}或者${cookie["username"]["value"]}

注意:
java中Cookie的api
getName():获取cookie的名称
getValue():获取cookie的value值
我们称name和value是cookie的bean属性,使用el表达式的时候可以省略书写get ,同理其他内置对象的属性也可以这样处理.


demo示例:

<%@ 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>el内置对象</title>
</head>
<body>

	<!--  1.pageContext : 得到web应用路径-->
	<%--方式1:通过jsp表达式获取 --%>
	<%=((HttpServletRequest) pageContext.getRequest()).getContextPath()%><br />
	<%--方式2:通过el表达式获取 --%>
	<a href="${pageContext.request.contextPath }/el.jsp">超链接</a><br />
	
	<!-- 2.获取域中的数据  -->
        <%
    	    pageContext.setAttribute("name","哈哈");
    	    request.setAttribute("name","呵呵");
  	    session.setAttribute("name","嘻嘻");
  	    application.setAttribute("name","嘿嘿");
        %>
        ${pageScope["name"]}<br/>
        ${requestScope["name"]}<br/>
   	${sessionScope["name"]}<br/>
   	${appliationScope["name"]}<br/>
	
	<!-- 3.param 和 paramValue: 得到局部参数数据  -->
	<%--方式1:通过jsp表达式获取 --%>
	<%=request.getParameter("name") %><br/>
	<%
		String[] names = request.getParameterValues("name");
		String[] ages = request.getParameterValues("age");
		out.write(names[0]);
		out.write(ages[0]);
	 %>
	<br/>
	<%--方式2:通过el表达式获取 --%>
	${param['name'] } - ${param['age'] }<br/>
	${paramValues['name'][0] } - ${paramValues['name'][1] }<br/>
	
	
	<!-- 4.header 和 headerValues: 得到请求头信息 -->
	<%--方式1:通过jsp表达式获取 --%>
	<%=request.getHeader("user-agent") %><br/>
	<%--方式2:通过el表达式获取 --%>
	${header['user-agent'] }<br/>
	${headerValues['user-agent'][0] }<br/>
	
	
	<!-- 5.cookie: 得到浏览器保存的cookie信息 -->
	<%--方式1:通过jsp表达式获取 --%>
	<%=request.getCookies()[0].getName() %> - <%=request.getCookies()[0].getValue() %><br/>
	<%--方式2:通过el表达式获取--%>
	${cookie['JSESSIONID'].name } - ${cookie['JSESSIONID'].value }<br/>
	<%--方式3:通过el表达式--%>
	${cookie.JSESSIONID.name}  -  ${cookie.JSESSIONID.value}<br/>
	
	<!-- 6.initParam: 得到web应用的全局参数 -->
	<%--方式1:通过jsp表达式获取 --%>
	<%=application.getInitParameter("AAA") %> - <%=application.getInitParameter("BBB") %><br/>
	<%--方式2:通过el表达式获取 --%>
	${initParam['AAA'] } - ${initParam['BBB'] }
	
</body>
</html>

猜你喜欢

转载自blog.csdn.net/mchenys/article/details/80988362