一 介绍
JSP表达式语言(Expression Language),简称EL,为存取变量、表达式运算和读取内置对象等内容提供了新的操作方式。目的:为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法。
${ param. username } 等价于 <%=request. getParameter(“username”) %>
${ requestScope.userlist } 等价于 <%=request.getAttribute(“userlist”) %>
同样,${ sessionScope.user.name } 等价于 ${ sessionScope.user.[“name”] } 或者 ${ sessionScope.users[0].name }
二 EL隐式对象
EL中属性的范围
属性范围 | EL中的名称 |
Page | PageScope |
Request | RequestScope |
Session | SessionScope |
Application | ApplicationScope |
EL表达式还有一些隐式的对象,一共11个,具体如下:
EL中隐含的对象
隐含对象 | 说明 |
pageScope | 取得Page范围属性名称中的值 |
requestScope | 取得Request范围属性名称中的值 |
sessionScope | 取得Session范围属性名称中的值 |
applicationScope | 取得Application范围属性名称中的值 |
pageContext | 表示JSP中的PageContext |
param | 同:ServletRequest.getParameter(String name) |
paramValues | 同:ServletRequest.getParameterValues(String name) |
header | 同:ServletRequest.getHeader(String name) |
headervalues | 同:ServletRequest.getHeaders(String name) |
cookie | 同:ServletRequest.getCookies( ) |
initparam | 同:ServletRequest.getInitParameter(String name) |
三 关于EL隐式对象的简单实例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>EL表达式简单实例</title>
</head>
<body>
<form action="elshow.jsp" method="post">
<table width="752" height="367" border="1">
<tr>
<td width="71">姓名:</td>
<td width="252"><label for="textfield"></label> <input
type="text" name="name" id="textfield" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="sex" id="radio" value="男" /> <label
for="radio"> 男 <input type="radio" name="sex" id="radio2"
value="女" /> 女
</label></td>
</tr>
<tr>
<td>年龄:</td>
<td><label for="textfield2"></label> <input name="age"
type="text" id="textfield2" size="5" /></td>
</tr>
<tr>
<td>爱好:</td>
<td><table width="526" height="90" border="0">
<tr>
<td width="116"><label> <input type="checkbox"
name="interest" value=" 打篮球" id="interest_0" /> 打篮球
</label> <br /></td>
<td width="105"><input type="checkbox" name="interest"
value="网上冲浪" id="interest_1" /> 网上冲浪</td>
<td width="68"><input type="checkbox" name="interest"
value="旅游" id="interest_2" /> 旅游</td>
<td width="66"><input type="checkbox" name="interest"
value="健身" id="interest_3" /> 健身</td>
</tr>
</table>
<p>
<br />
</p></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="button" id="button"
value="提交" /></td>
</tr>
</table>
</form>
</body>
</html>
elshow.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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">
<base href="<%=basePath%>">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
request.setAttribute("r", "requsetTest");
session.setAttribute("s", "sessionTest");
application.setAttribute("a", "applicationTest");
pageContext.setAttribute("p", "pageContextTest");
%>
姓名:${param.name}<br>
性别:${param.sex}<br>
年龄:${param.age}<br>
爱好:
<c:forEach items="${paramValues.interest}" var="interest">
${interest}
</c:forEach><br>
<hr/>
request示例:<br/>
${requestScope.r}<br/> <hr/>
session示例:<br/>
${sessionScope.s}<br/> <hr/>
application示例:<br/>
${applicationScope.a}<br/> <hr/>
pageContext示例:<br/>
${pageScope.p}<br/>
<!-- pageContext用于取得其他有关用户要求或页面的详细信息 -->
${pageContext.request.queryString} <br /><hr /> <!-- 取得请求的参数字符串 -->
${pageContext.request.requestURL} <br /><hr /> <!-- 取得请求的URL,不包括参数字符串 -->
${pageContext.request.contextPath} <br /><hr /> <!-- web application的名称 -->
${pageContext.request.method} <br /><hr /> <!-- 取得提交的HTTP方法(PS:GET或POST) -->
${pageContext.request.protocol} <br /><hr /> <!-- 取得使用的协议 -->
${pageContext.request.remoteUser} <br /><hr /> <!-- 取得客户端的名称 -->
${pageContext.request.remoteAddr} <br /><hr /> <!-- 取得客户端的IP地址 -->
${pageContext.session.id} <br /><hr /> <!-- 取得session 的ID -->
${pageContext.servletContext.serverInfo} <br /><hr /> <!-- 取得客户端的服务信息 -->
</body>
</html>
测试效果:
(PS:欢迎大家来我的个人博客踩踩:http://www.zifangsky.cn/)
转载于:https://blog.51cto.com/983836259/1741856