JSP内置对象的范围和属性
JSP内置对象的范围和属性
●●●范围(Scope)
有些JSP程序员会将request、session、application和pageContext归为一类,缘由在于:它们皆能借助setAttribute()和getAttribute()来设定和取得其属性(Attribute),经过这两种方法来做到数据分享。
咱们先来看下面这段小程序:
Page1.jsp
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>Page1.jsp</title>
</head>
<body>
</br>
<%
application.setAttribute("Name","mike");
application.setAttribute("Password","browser");
%>
<jsp:forwardpage="Page2.jsp"/>
</body>
</html>
在这个程序中,咱们设定两个属性:Name、Password,其值为:mike、browser。然后再转交(forward)到Page2.jsp。我只需在Page2.jsp当中退出application.getAttribute(),就能取得在Page1.jsp设定的数据。
Page2.jsp
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>Page2.jsp</title>
</head>
<body>
<%
StringName=(String)application.getAttribute("Name");
StringPassword=(String)application.getAttribute("Password");
out.println("Name="+Name);
out.println("Password="+Password);
%>
</body>
</html>
执行后果为:
Name=mikePassword=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)JSPScope—Page
所谓的Page,指的是单单一页JSPPage的范围。若要将数据存入Page范围时,可能用pageContext对象的setAttribute()方法;若要取得Page范围的数据时,可能利用pageContext对象的getAttribute()方法。咱们将之前的范例做小幅度的修正,将application改为pageContext。
PageScope1.jsp
<%@pagecontentType="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:forwardpage="PageScope2.jsp"/>
</body>
</html>
PageScope2.jsp
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>CH5-PageScope2.jsp</title>
</head>
<body>
<h2>Page范围-pageContext</h2>
</br>
<%
StringName=(String)pageContext.getAttribute("Name");
StringPassword=(String)pageContext.getAttribute("Password");
out.println("Name="+Name);
out.println("Password="+Password);
%>
</body>
</html>
执行后果为:
Name=nullPassword=null
这个范例程序和之前有点类似,只是之前的程序是application,如今改为pageContext,然而后果却大不相反,PageScope2.jsp基本无奈取得PageScope1.jsp设定的Name和Password值,由于在PageScope1.jsp当中,是把Name和Password的属性范围设为Page,所以Name和Password的值只能在PageScope1.jsp当中取得。若修正PageScope1.jsp的程序,重新命名为PageScope3.jsp,如下:
PageScope3.jsp
<%@pagecontentType="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");
StringName=(String)pageContext.getAttribute("Name");
StringPassword=(String)pageContext.getAttribute("Password");
out.println("Name="+Name);
out.println("Password="+Password);
%>
</body>
</html>
PageScope3.jsp的执行后果为:
Name=mikePassword=browser
通过修正后的程序,Name和Password的值就能顺利显示进去。这个范例次要用来阐明一个概念:若数据设为Page范围时,数据只能在同一个JSP网页上取得,其余JSP网页却无奈取得该数据。
(2)JSPScope—Request
Request的范围是指在一JSP网页收回申请到另一个JSP网页之间,随后这个属性就失效。设定Request的范围时可应用request对象中的setAttribute()和getAttribute()。咱们再来看下列这个范例:
RequestScope1.jsp
<%@pagecontentType="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:forwardpage="RequestScope2.jsp"/>
</body>
</html>
RequestScope2.jsp
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>RequestScope2.jsp</title>
</head>
<body>
<h2>Request范围-request</h2>
<%
StringName=(String)request.getAttribute("Name");
StringPassword=(String)request.getAttribute("Password");
out.println("Name="+Name);
out.println("Password="+Password);
%>
</body>
</html>
执行后果为:
Name=mikePassword=browser
RequestScope1.jsp转向到RequestScope2.jsp时,RequestScope2.jsp也能取得RequestScope1.jsp设定的Name和Password值。不过其余的JSP网页无奈失去Name和Password值,除非它们也和RequestScope1.jsp有申请的关系。
除了应用转向(forward)的方法可能存取request对象的数据之外,还能利用蕴含(include)的方法。
假若我将RequestScope1.jsp的
<jsp:forwardpage="RequestScope2.jsp"/>
改为
<jsp:includepage="RequestScope2.jsp"flush="true"/>
执行后果还是一样的。示意利用<jsp:include>标签所蕴含出去的网页,异样也可能取得Request范围的数据。
(3)JSPScope—Session、Application
下表引见了最后两种范围:Session、Application。
●●●属性(Attribute)
下表列出了普通贮存和取得属性的方法,以下pageContext、request、session和application皆可利用(留意:pageContext并无getAttributeNames()方法)。
当咱们利用ObjectgetAttribute(Stringname)取得name属性的值时,它会回传一个java.lang.Object,因此,咱们还必须依据name属性值的类型做转换类型(Casting)的工作。
例如,若要取得String类型的Name属性时:
StringName=(String)Sessio.getAttribute("Name");
若要取得Integer类型的Year属性时:
IntegerYear=(Integer)session.getAttribute("Year");
假若咱们的数据要设为Page范围时,则只有要:
pageContext.setAttribute("Year",newInteger(2001));
若要为Request、Session或Application时,就分别存入request、session或application对象之中,如下:
request.setAttribute("Month",newInteger(12));
session.setAttribute("Day",newInteger(27));
application.setAttribute("Times",newInteger(10));