再次复习jsp中的四种属性范围

1、page属性范围
       在一个JSP页面上设置的属性只能在一个页面取得,跳转到其他页面则此属性消失。
PageScopeDemo01.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         pageContext.setAttribute("uname","HELLO") ;
         pageContext.setAttribute("udate",new Date()) ;
         // 取得属性
         String name = (String)pageContext.getAttribute("uname") ;
         Date date = (Date)pageContext.getAttribute("udate") ;
%>
<h1>name --> <%=name%></h1>
<h1>date --> <%=date%></h1>
       当前页可以取得所设置的属性,下面对页面进行修改,让其完成一此跳转。
PageScopeDemo02.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         pageContext.setAttribute("uname","HELLO") ;
         pageContext.setAttribute("udate",new Date()) ;
%>
<jsp:forward page="PageScopeDemo03.jsp"/>
PageScopeDemo03.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 取得属性
         String name = (String)pageContext.getAttribute("uname") ;
         Date date = (Date)pageContext.getAttribute("udate") ;
%>
<h1>name --> <%=name%></h1>
<h1>date --> <%=date%></h1>
       证明:一个页面上设置的属性,只能在本页面中取得。如果现在希望可以在跳转页中取得
,则可以扩大范围到request范围上去。
2、request属性范围
       request可以把属性保存在一次服务器跳转范围之中。
RequestScopeDemo01.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         request.setAttribute("uname","HELLO") ;
         request.setAttribute("udate",new Date()) ;
%>
<jsp:forward page="RequestScopeDemo02.jsp"/>
RequestScopeDemo02.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 取得属性
         String name = (String)request.getAttribute("uname") ;
         Date date = (Date)request.getAttribute("udate") ;
%>
<h1>name --> <%=name%></h1>
<h1>date --> <%=date%></h1>
但是,需要注意的是,如果使用了下面的跳转形式,则不能够取得:
RequestScopeDemo03.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         request.setAttribute("uname","HELLO") ;
         request.setAttribute("udate",new Date()) ;
%>
<a href="RequestScopeDemo02.jsp">RequestScopeDemo02.jsp</a>
       发现以上程序执行之后,地址栏改变了,但是设置的属性也消失了。
       如果现在希望无论怎样跳转,属性都可以被保存下来,则就要扩大到session范围
3、session属性范围
       session属性范围无论页面怎样跳转,都可以保存下来,但是只针对于同一个浏览器打开的
相关页面。
SessionScopeDemo01.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         session.setAttribute("uname","HELLO") ;
         session.setAttribute("udate",new Date()) ;
%>
<a href="SessionScopeDemo02.jsp">SessionScopeDemo02.jsp</a>
SessionScopeDemo02.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 取得属性
         String name = (String)session.getAttribute("uname") ;
         Date date = (Date)session.getAttribute("udate") ;
%>
<h1>name --> <%=name%></h1>
<h1>date --> <%=date%></h1>
       可以把每一个用户理解为一个session。如果现在希望所有的用户都可以看见,则就可以继
续扩大范围 —— application范围。
4、application属性范围
       application范围,是把属性设置在整个服务器上,所有的用户都可以进行访问。
ApplicationScopeDemo01.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         application.setAttribute("uname","HELLO") ;
         application.setAttribute("udate",new Date()) ;
%>
ApplicationScopeDemo02.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 取得属性
         String name = (String)application.getAttribute("uname") ;
         Date date = (Date)application.getAttribute("udate") ;
%>
<h1>name --> <%=name%></h1>
<h1>date --> <%=date%></h1>
       如果服务器一关闭,则所有的application属性都消失,属性保存在服务器之上,如果此属
性保存的内容过多,则肯定会直接影响到服务器的性能。所以一般尽可能少设置application属性

5、pageContext对象的补充
       JSP中如果要想设置一个page范围的属性则一定使用pageContext对象,实际上对于整个JSP
代码,所有的四种属性范围都是可以通过pageContext对象进行设置的,因为在此对象中的设置属
性方法有两个:
       · public void setAttribute(String name,Object attribute) à 设置的属性默认为
page范围
· public void setAttribute(String name,Object o,int scope) à 可以指定属性的范围
       |- 在PageContext类中提供了以下几个常量:
              |- 表示page范围:public static final int PAGE_SCOPE
              |- 表示request范围:public static final int REQUEST_SCOPE
              |- 表示session范围:public static final int SESSION_SCOPE
              |- 表示application范围:public static final int APPLICATION_SCOPE
下面通过pageContext对象设置一个request范围的属性
RequestScopeDemo04.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.*"%>
<%
         // 设置两个属性
         pageContext.setAttribute("uname","HELLO",PageContext.REQUEST_SCOPE) ;
         pageContext.setAttribute("udate",new Date(),PageContext.REQUEST_SCOPE) ;
%>
<jsp:forward page="RequestScopeDemo02.jsp"/>
使用的原则:
       · 因为四种属性范围的保存时间不同,所以占用内存的时间也就不同了,能使用request
就不要使用session,一般情况下,request和session两个属性范围使用的频率是最高的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章