一、作用域访问对象
对象名称 | 说明 |
pageScope | 与页面作用域(page)中的属性相关联的Map类 |
requestScope | 与请求作用域(request)中的属性相关联的Map类 |
sessionScope | 与会话作用域(session)中的属性相关联的Map类 |
application | 与应用程序作用域(application)中的属性相关联的Map类 |
当使用EL表达式访问某个属性值时,应该指定查找的范围,如${requestScope.employee},即在请求(request)范围内查找属性employee的值。如果程序中不指定查找范围,则系统就会按照page—>request—>session—>application的顺序查找。
二、参数访问对象
对象名称 | 说明 |
param | 按照参数名称访问单一请求值的Map对象 |
paramValues | 按照参数名称访问数组请求值的Map对象 |
三、JSP隐式对象
为了能够方便地访问JSP隐式对象,EL表达式语言引入了pageContext,它是JSP和EL的一个公共对象,通过pageContext可以访问其他八个JSP内置对象(request、response等),这也是EL表达式语言把它作为内置对象的一个主要原因。
对象名称 | 说明 |
pageContext | 提供对页面信息和JSP内置对象的访问 |
示例:一个注册页面(register.jsp),一个注册成功页面(regSuccess.jsp),注册成功之后,注册成功页面显示注册信息。
注册页面(register.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HMTL4.01 Transitional//EN">
<html>
<head><title>用户注册</title></head>
<body>
<form id="regFrm" action="regSuccess.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="userName" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td><input id="password" name="password" type="password"></td>
</tr>
<tr>
<td>业余爱好:</td>
<td>
<input name="habit" type="checkbox" value="Reading">看书
<input name="habit" type="checkbox" value="Game">玩游戏
<input name="habit" type="checkbox" value="Travelling">旅游
<input name="habit" type="checkbox" value="Swimming">游泳
<input name="habit" type="checkbox" value="TV">看电视
</td>
</tr>
<tr colspan="2">
<input type="submit" value="提交">
</tr>
</table>
</form>
</body>
</html>
注册成功页面(regSuccess.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="org.jbit.bean.User"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
request.setCharacterEncoding("UTF-8");
//从请求参数中取得用户名
String userName = request.getParameter("userName");
//密码
String password = request.getParameter("password");
//业余爱好
String[] habits = request.getParameterValues("habit");
//此处生成一个User对象,以便展示EL访问某个作用域内的对象
User user = new User();
user.setName(userName);
user.setPassword(password);
user.setHabits(habits);
//把此user对象设置为request范围内的一个属性
request.setAttribute("userObj", user);
%>
//省略其他代码
====使用request作用域内的userObj对象展示注册信息=====<br>
注册成功,您的注册信息是:<br>
用户名:${requestScope.userObj.name}<br>
业余爱好:
<%
for(int i = 0 ; i < habits.length; i++){
if(i > 0){
out.print("、");
}
out.print(habits[i]);
}
%>
<br><br><br><br>
====使用param对象与paramValues对象展示注册信息====<br>
用户名:${param.userName}<br>
业余爱好:
<%
for (int i = 0; i < habits.length; i++){
if (i > 0) {
out.print("、");
}
out.print(paramValues.habit[i]);
}
%>
//省略其他代码
转载于:https://blog.51cto.com/msuccess/1257697