首页 > 代码库 > JSP详细篇——EL表达式(二)
JSP详细篇——EL表达式(二)
EL的隐含对象
为了能够获得Web应用中的是相关数据,EL提供了11个隐含对象,这些对象类似于JSP的内置对象,也是直接通过对象名进行操作。在EL的隐含对象中,除了PageContext是JavaBean对象,对应于javax.servlet.jsp.PageContext类型外,其他的隐含对象都对应于java.util.Map类型。这些隐含对象可以分为页面上下文对象、访问作用域范围的隐含对象和访问环境信息的隐含对象3种。下面分别进行详细介绍。
1、页面上下文对象
页面上下文对象为pageContext,用于访问JSP内置对象(request/response/out/session/exception和page,但是不能用于获取application/config/pageContext)和servletContext。在获取到这些内置对象后,就可以获取其属性。这些属性与对象的getXXX()方法相对应,在使用时,去掉方法名中的get,并将首字母改为小写即可。
访问request对象
通过pageContext获取JSP内置对象中的request对象,可以使用以下语句:
${pageContext.request}
获取到request对象后,就可以通过该对象获取与客户端相关的信息。例如:HTTP报头信息、客户信息提交方式、客户端IP地址和端口号等。
范例:
要访问getServerPort()方法,可以使用以下的代码:
${pageContext.request.serverPort}
以上代码将返回端口号。
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘request.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${pageContext.request.serverName }<br/>
${pageContext.request.serverPort }<br/>
${pageContext.request.servletPath }<br/>
</body>
</html>
访问response对象
通过pageContext获取JSP内置对象中的response对象,可以使用下面的语句:
${pageContext.response}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘response.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.response.bufferSize }<br/>
${pageContext.response.characterEncoding }
</center>
</body>
</html>
访问out对象
通过pageContext访问out对象,使用以下语法:
${pageContext.out}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘out.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.out.bufferSize }<br/>
${pageContext.out.remaining}
</center>
</body>
</html>
访问session对象
访问session对象的语法格式为:
${pageContext.session}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘session.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.session.id }
</center>
</body>
</html>
访问exception对象
通过pageContext获取JSP内置对象的exception对象的语法个格式为:
${pageContext.exception}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘exception.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.exception.localizedMessage }
</center>
</body>
</html>
访问page对象
通过pageContext访问page对象的语法格式为:
${pageContext.page}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘page.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.page.class }
</center>
</body>
</html>
访问servletContext对象
语法格式如下:
${pageContext.servletComtext}
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘servletContext.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
${pageContext.servletContext.contextPath }
</center>
</body>
</html>
2、访问作用域范围的隐含对象
在EL中提供了4种用于访问作用域范围的隐含对象,即pageScope/requestScope/sessionScope/applicationScope。应用这4个隐含对象指定所要查找的标识符的作用域后,系统将不再按照默认的顺序(page/request/session/application)来查找相应的标识符。他们与JSP中的page/request/session/application内置对象相似。不过,这4个隐含对象只能用于取得指定范围内的属性,而不能取得其他相关信息。
pageScope隐含对象
pageScope隐含对象用于返回包含page范围的属性值的集合,返回值为java.util.Map对象。
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘pageScope.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" scope="page" class="com.zgy.elbean.User" >
<jsp:setProperty name="user" property="name" value="java"/>
</jsp:useBean>
<center>
${pageScope.user.name }
</center>
</body>
</html>
requestScope隐含对象
requestScope隐含对象用于返回包含request范围内的属性的集合。返回值为java.util.Map对象。
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘requestScope.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" scope="request" class="com.zgy.elbean.User" >
<jsp:setProperty name="user" property="name" value="JSP"/>
</jsp:useBean>
<center>
${requestScope.user.name }
</center>
</body>
</html>
sesssionScope隐含对象
sessionScope隐含对象用于返回session范围内的属性值的集合。返回值为java.util.Map对象。
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘sessionScope.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" scope="session" class="com.zgy.elbean.User" >
<jsp:setProperty name="user" property="name" value="JSP"/>
</jsp:useBean>
<center>
${sessionScope.user.name }
</center>
</body>
</html>
applicationScope隐含对象
applicationScope隐含对象用于返回包含application范围的属性值的集合,返回值为java.util.Map对象
范例:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘applicationScope.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" scope="application" class="com.zgy.elbean.User" >
<jsp:setProperty name="user" property="name" value="JavaScript"/>
</jsp:useBean>
<center>
${applicationScope.user.name }
</center>
</body>
</html>
3、访问环境信息的隐含对象
在EL中提供了6个访问环境信息的隐含对象。
A、param 对象
param 对象用于获取请求参数的值,应用在参数值只有一个的情况。在应用param对象时,返回的结果为字符串。
范例:
在JSP页面中,放置一个名为user的文本框:
<input name = “name” type = “text”>
当表单提交后,要获取name文本框的值,使用如下方式:
${param.name}
PS:
在name文本框中如果输入了中文,那么在使用EL输出其内容的时候,要使用request.setCharacterEncoding(“GBK”);语句设置请求的编码为支持中文的编码,不然将出现乱码。
B、paramValues对象
如果一个请求参数名对应多个值,则需要使用paramValues对象获取请求的参数值。在应用paramValues对象时,返回的结果是数组。
范例:
在JSP页面中,放置一个名称为affect的复选框。
<input name=”affect” type=”checkbox” id=”affect” value=http://www.mamicode.com/”登山”>
登山
<input name=”affect” type=”checkbox” id=”affext” value=http://www.mamicode.com/”游泳”>
游泳
<input name=”affect” type=”checkbox” id=”affext” value=http://www.mamicode.com/”慢走”>
慢走<input name=”affect” type=”checkbox” id=”affext” value=http://www.mamicode.com/”晨跑”>
晨跑
当提交表单后,要获取affect的值,可以使用下面的形式:
<%request.setCharacterEncoding(“UTF-8”);%>
爱好为:${paramValues.affect[0]}${paramValues.affect[1]}${paramValues.affect[2]}${paramValues.affect[3]}
在使用param和paramValues对象时,如果指定的参数不存在,则返回空字符串而不是返回null
C、header和headerValues对象
header对象用于获取HTTP请求的一个具体的header的值,但是在有些情况下,可能窜在同一个header拥有多个不同的值的情况,这就要使用到headerValues对象
范例:
要获取HTTP请求的header的connection属性,可以使用如下形式:
${header.connection}或者${header[“connection]}
但是,如果要获取HTTP请求的header的user-agent属性,则必须使用以下EL表达式:
${header[“user-agent”]}
D、initParam对象
initParam对象用于获取Web应用的初始化参数的值
范例:
在Web应用的web.xml中设置一个初始化参数author,用于指定作者。
<context-param>
<param-name>author</param-name>
<param-value>mr</param-value>
</context-param>
运用EL表达式获取参数author:
${initParam.author}
F、cookie对象
在EL中没有提供向cookie中保存值的方法,但是提供了访问由请求设置的cookie方法,这可以通过cookie隐含对象来实现。如果在cookie中已经设定好了一个名称为username的值,那么可以通过${cookie.username}来获取该cookie对象。如果要获取cookie中的值,需要使用cookie对象的value属性
范例:
使用response对象设置一个请求有效的cookie对象,然后在使用EL获取该cookie对象的值,可以使用以下代码:
<%
Cookie cookie=new Cookie(“user”,”zhangsan”);
response.addCookie(cookie);
%>
${cookie.user.value}
JSP详细篇——EL表达式(二)