表达式语言的内置对象定义:
表达式内置对象 说明
pageContext 表示javax.servlet.jsp.PageContext对象
pageScope 表示从page属性范围查找输出属性
requestScope 表示从request属性范围查找输出属性
sessionScope 表示从session属性范围查找输出属性
applicationScope 表示从application属性范围查找输出属性
param 接收传递到本页面的参数
paramValues 接收传递到本页面的一组参数
header 取得一个头信息数据
headerValues 取得一组头信息数据
cookie 取出cookie中数据
initParam 取得配置的初始化参数
如果现在在不同的属性范围之中,保存了同名的属性,则EL的查找范围
就是page-->request-->session-->application;
<%@ page contentType="text/html" pageEncoding="gbk"%>
<html>
<head><title>这是测试</title></head>
<body>
<%
pageContext.setAttribute("info","page属性范围");
request.setAttribute("info","request属性范围");
session.setAttribute("info","session属性范围");
application.setAttribute("info","application属性范围");
%>
<h3>${info}</h3>
</body>
</html>
这是输出:"page属性范围"
属性范围:
属性范围 范例 说明
pageScope ${pageScope.属性} 取得page范围的属性
requestScope ${requestScope.属性} 取得request范围的属性
sessionScope ${sessionScope.属性} 取得session范围的属性
applicationScope ${applicationScope.属性} 取得application范围的属性
<%@ page contentType="text/html" pageEncoding="gbk"%>
<html>
<head><title>这是测试</title></head>
<body>
<%
pageContext.setAttribute("info","page属性范围");
request.setAttribute("info","request属性范围");
session.setAttribute("info","session属性范围");
application.setAttribute("info","application属性范围");
%>
<h3>PAGE属性内容:${pageScope.info}</h3>
<h3>REQUEST属性内容:${requestScope.info}</h3>
<h3>SESSION属性内容:${sessionScope.info}</h3>
<h3>APPLICATION属性内容:${applicationScope.info}</h3>
</body>
</html>
在EL的内置对象中可以发现pageContext,之前在讲解内置对象的时候一直强调,pageContext可以取得request、
session、application等对象,现在就可以通过EL直接完成内置对象的方法调用;
<%@ page contentType="text/html" pageEncoding="gbk"%>
<html>
<head><title>这是测试</title></head>
<body>
<h3>IP地址:${pageContext.request.remoteAddr}</h3>
<h3>SESSION ID:${pageContext.session.id}</h3>
<h3>是否是新session:${pageContext.session.new}</h3>
</body>
</html>
表单可以传递参数,或者是通过URL重写传递参数。如果要想接受参数,"param"对象完成;
<%@ page contentType="text/html" pageEncoding="gbk"%>
<html>
<head><title>这是测试</title></head>
<body>
<h3>通过内置对象接受输入参数:<%=request.getParameter("ref")%></h3>
<h3>通过表达式语言接受输入参数:${param.ref}</h3>
</body>
</html>
也可以完成一组参数的接受;
param_values_demo.html:
<html>
<head><title>这是测试</title></head>
<body>
<form action="param_value_demo.jsp" method="post">
兴趣:<input type="checkbox" name="inst" value="唱歌">唱歌
<input type="checkbox" name="inst" value="游泳">游泳
<input type="checkbox" name="inst" value="看书">看书
<input type="submit" value="显示">
</form>
</body>
</html>
现在JSP接收的时候,肯定要使用paramValues接收一组参数,由于现在接受过来的是数组,如果是
数组的话则需要下标访问;
param_values_demo.jsp:
<%@ page contentType="text/html" pageEncoding="gbk"%>
<html>
<head><title>这是测试</title></head>
<body>
<%
request.setCharacterEncoding("gbk");
%>
<h3>第一个参数:${paramValues.inst[0]}</h3>
<h3>第二个参数:${paramValues.inst[1]}</h3>
<h3>第三个参数:${paramValues.inst[2]}</h3>
</body>
</html>
但是在开发中用的最多的还是param对象,接受一个参数,因为从MVC设计模式来看,所有的参数
接受应该交给Servlet;
注意:${param.info}和${info}的区别:
·${param.info}:取得的是参数的值,<input type="text" name="info">
·${info}: 取得的是属性的值,如:request.setAttribute("info","这才是属性");
05---表达式语言02(表达式语言的内置对象)
最新推荐文章于 2022-11-14 10:21:27 发布