JSP内置对象的范围和属性-初学必看
来源:互联网 发布:谈谈你对java的理解 编辑:程序博客网 时间:2024/05/16 12:57
JSP内置对象的范围和属性
关键词: JSP内置对象
●●●范围( Scope )
有些 JSP 程序员会将 request、session、application 和 pageContext 归为一类,原因在于:它们皆能借助 setAttribute( )和 getAttribute( )来设定和取得其属性(Attribute),通过这两种方法来做到数据分享。
我们先来看下面这段小程序:
Page1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>Page1.jsp</title>
</head>
<body>
</br>
<%
application.setAttribute("Name","mike");
application.setAttribute("Password","browser");
%>
<jsp:forward page="Page2.jsp"/>
</body>
</html>
在这个程序中,我们设定两个属性:Name、Password,其值为:mike、browser。然后再转交(forward)到Page2.jsp。我只要在Page2.jsp当中加入 application.getAttribute( ),就能取得在Page1.jsp设定的数据。
Page2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>Page2.jsp</title>
</head>
<body>
<%
String Name = (String) application.getAttribute("Name");
String Password = (String) application.getAttribute("Password");
out.println("Name = "+Name);
out.println("Password = "+ Password);
%>
</body>
</html>
执行结果为:
Name=mike Password=browser
由这个例子可以看出,JSP提供给开发人员一项传递数据的机制,那就是利用setAttribute( )和getAttribute( )方法,如同Page1.jsp和Page2.jsp的做法。
在上面Page1.jsp和Page2.jsp的程序当中,是将数据存入到 application 对象之中。除了application之外,还有request、pageContext和 session,也都可以设定和取得属性值,那它们之间有什么分别吗?
它们之间最大的差别在于范围(Scope)不一样,这个概念有点像 C、C++中的全局变量和局部变量的概念。接下来就介绍JSP的范围。
JSP有四种范围,分别为Page、Request、Session、Application。
(1)JSP Scope—Page
所谓的 Page,指的是单单一页JSP Page的范围。若要将数据存入 Page 范围时,可以用pageContext对象的 setAttribute()方法;若要取得Page范围的数据时,可以使用 pageContext对象的getAttribute( )方法。我们将之前的范例做小幅度的修改,将application改为 pageContext。
PageScope1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>PageScope1.jsp</title>
</head>
<body>
<h2>Page 范围 - pageContext</h2>
<%
pageContext.setAttribute("Name","mike");
pageContext.setAttribute("Password","browser");
%>
<jsp:forward page="PageScope2.jsp"/>
</body>
</html>
PageScope2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>CH5 - PageScope2.jsp</title>
</head>
<body>
<h2>Page 范围 - pageContext</h2>
</br>
<%
String Name = (String)pageContext.getAttribute("Name");
String Password = (String)pageContext.getAttribute("Password");
out.println("Name = "+Name);
out.println("Password = "+ Password);
%>
</body>
</html>
执行结果为:
Name=null Password=null
这个范例程序和之前有点类似,只是之前的程序是 application,现在改为 pageContext,但是结果却大不相同,PageScope2.jsp 根本无法取得 PageScope1.jsp 设定的 Name 和 Password 值,因为在PageScope1.jsp当中,是把Name和Password 的属性范围设为Page,所以Name 和Password 的值只能在PageScope1.jsp当中取得。若修改PageScope1.jsp的程序,重新命名为PageScope3.jsp,如下:
PageScope3.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>CH5 - PageScope3.jsp</title>
</head>
<body>
<h2>Page 范围 - pageContext</h2>
</br>
<%
pageContext.setAttribute("Name","mike");
pageContext.setAttribute("Password","browser");
String Name = (String)pageContext.getAttribute("Name");
String Password = (String)pageContext.getAttribute("Password");
out.println("Name = "+Name);
out.println("Password = "+ Password);
%>
</body>
</html>
PageScope3.jsp的执行结果为:
Name=mike Password=browser
经过修改后的程序,Name 和 Password 的值就能顺利显示出来。这个范例主要用来说明一个概念:若数据设为Page范围时,数据只能在同一个 JSP网页上取得,其他 JSP网页却无法取得该数据。
(2)JSP Scope—Request
Request 的范围是指在一JSP网页发出请求到另一个JSP网页之间,随后这个属性就失效。设定 Request 的范围时可利用 request 对象中的setAttribute()和getAttribute()。我们再来看下列这个范例:
RequestScope1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>CH5 - RequestScope1.jsp</title>
</head>
<body>
<h2>Request 范围 - request</h2>
<%
request.setAttribute("Name","mike");
request.setAttribute("Password","browser");
%>
<jsp:forward page="RequestScope2.jsp"/>
</body>
</html>
RequestScope2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>RequestScope2.jsp</title>
</head>
<body>
<h2>Request 范围 - request</h2>
<%
String Name = (String) request.getAttribute("Name");
String Password = (String) request.getAttribute("Password");
out.println("Name = "+Name);
out.println("Password = "+ Password);
%>
</body>
</html>
执行结果为:
Name=mike Password=browser
RequestScope1.jsp转向到RequestScope2.jsp时, RequestScope2.jsp也能取得RequestScope1.jsp设定的Name和Password值。不过其他的JSP网页无法得到Name和Password值,除非它们也和RequestScope1.jsp有请求的关系。
除了利用转向(forward)的方法可以存取 request 对象的数据之外,还能使用包含(include)的方法。
假若我将RequestScope1.jsp的
<jsp:forward page="RequestScope2.jsp"/>
改为
<jsp:include page="RequestScope2.jsp" flush="true"/>
执行结果还是一样的。表示使用<jsp:include>标签所包含进来的网页,同样也可以取得Request范围的数据。
(3)JSP Scope—Session、Application
下表介绍了最后两种范围:Session、Application。
●●●属性(Attribute)
下表列出了一般储存和取得属性的方法,以下 pageContext、request、session和 application皆可使用(注意:pageContext并无getAttributeNames( )方法)。
当 我 们 使 用Object getAttribute(String name)取得 name 属 性 的 值 时 ,它 会 回 传 一 个java.lang.Object,因此,我们还必须根据name属性值的类型做转换类型(Casting)的工作。
例如,若要取得String类型的Name属性时:
String Name = (String)Sessio.getAttribute("Name");
若要取得Integer类型的Year属性时:
Integer Year = (Integer)session.getAttribute("Year");
假若我们的数据要设为Page范围时,则只需要:
pageContext.setAttribute("Year", new Integer(2001));
若要为 Request、Session 或 Application 时,就分别存入 request、session 或 application对象之中,如下:
request.setAttribute("Month", new Integer(12) );
session.setAttribute("Day", new Integer(27) );
application.setAttribute("Times", new Integer(10));
- JSP内置对象的范围和属性-初学必看
- JSP内置对象的范围和属性
- JSP内置对象(一)初学者必看
- JSP内置对象(二)初学者必看
- JSP内置对象(二)初学者必看
- Jsp四种内置对象的属性范围
- jsp内置对象:page属性范围
- jsp内置对象 的使用范围和类型【说明】
- JSP基础知识(application和内置对象的作用范围)
- JSP内置对象和属性
- JSP内置对象和属性
- JavaWeb——JSP内置对象application,JSP属性范围
- JSP 四个内置对象的保存范围
- jsp的对象和范围
- JSP 9大内置对象和属性
- jsp 内置 对象 属性
- Jsp内置对象范围大小和几个常用方法介绍
- JSP中的include指令、application内置对象、JSP中内置对象的范围
- 为kaine测试trackback
- linux学习(远程控制跟IP地址配置)2007.6.1
- asp.net(c#)中DataGrid问题
- 2007年3月18日实考高级口译听力 学习 (昂立学院口译项目部 网址:www.onlycollege.com.cn )
- 算法习题 (去掉数组重复元素 + 交换一串数组元素 )
- JSP内置对象的范围和属性-初学必看
- 尽量使用“引用常量”传参,而不是传值
- JSP内置对象(一)初学者必看
- JSP的工作原理
- Linux中的运行等级(run level)
- 动态创建DataList
- JSP内置对象(二)初学者必看
- HTTP/1.1 Cache-Control 标头的设定参数
- cakephp: 在ide中执行cakephp test case