public abstract void setAttribute(java.lang.String name,
java.lang.Object o,
int scope)
public static final int PAGE_SCOPE
public static final int REQUEST_SCOPE
public static final int SESSION_SCOPE
public static final int APPLICATION_SCOPE
,pageContext.PAGE_SCOPE
pageDemo03.jsp
<%
pageContext.setAttribute("name","MLDN",pageContext.PAGE_SCOPE);
pageContext.setAttribute("password","LXH",pageContext.PAGE_SCOPE);
%>
<jsp:forward page="pageDemo04.jsp"/>
pageDemo04.jsp
<%
//当前页可以取得
String name= (String)pageContext.getAttribute("name");
String password=(String)pageContext.getAttribute("password");
%>
<h1>name:<%=name%></h1>
<h1>password:<%=password%></h1>
输出: name:null
password:null
REQUEST.SCOPE
pageDemo03.jsp
<%
pageContext.setAttribute("name","MLDN",pageContext.REQUEST_SCOPE);
pageContext.setAttribute("password","LXH",pageContext.REQUEST_SCOPE);
%>
<jsp:forward page="pageDemo04.jsp"/>
pageDemo04.jsp
<%
//当前页可以取得
String name= (String)request.getAttribute("name");
String password=(String)request.getAttribute("password");
%>
<h1>name:<%=name%></h1>
<h1>password:<%=password%></h1>
输出: name:MLDN
password:LXH
SESSION_SCOP,APPLICATION_SCOP与REQUEST_SCOP用法,输出结果一样
总结:
四种属性范围都是依靠pageContext展开的,但是在开发往往使用session,requst范围最多,