jsp页面中有四种范围变量:request,session,application,pageContext。
这四种区别还是很好区分的,以我的思路方法:
有3个jsp页面:
p1.jsp
This is my p1 page. <br>
<%
request.setAttribute("scope", "request");
session.setAttribute("scope", "session");
application.setAttribute("scope", "application");
pageContext.setAttribute("scope","pagecomtext");
%>
<jsp:forward page="p2.jsp" ></jsp:forward>
<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><span></span></span></strong>
p2.jsp
This is my p2 page. <br>
request:<%= request.getAttribute("scope")%> <br>
session:<%= session.getAttribute("scope")%> <br>
application:<%= application.getAttribute("scope")%> <br>
pagecontext:<%= pageContext.getAttribute("scope")%> <br>
p3.jsp
This is my p3 page. <br>
request:<%= request.getAttribute("scope")%> <br>
session:<%= session.getAttribute("scope")%> <br>
application:<%= application.getAttribute("scope")%> <br>
pagecontext:<%= pageContext.getAttribute("scope")%> <br>
访问p1.jsp,会跳转到p2.jsp页面。同时request,session,application会有值,但是pageContext为null。为了方便打开另一个浏览器单独访问p2..jsp,
相当于另外一个用户,request,session,pageContext为null,application有值,关闭此浏览器再访问p3.jsp,request,session,pageContext为null,application
有值。如果在p3.jsp中
This is my p3 page. <br>
后添加代码
pageContext.setAttribute("scope","pagecomtext3");
访问p3.jsp时,application会有值。
从小到大范围:
- pageContext页面,只有在同一个页面内才会相同,尽管同一用户不同页面都不行。
- request请求,同一个用户,值要进行传递才相同,没有进行传递,其他页面也不会获得相应的值;
- session回话,同一回话;
- application项目,同一项目下,不同用户都拥有它,共享空间,并且值相同;
这是我学习时的理解,不知道好不好用,如有错误还请纠正。