jsp中四种范围变量

2 篇文章 0 订阅

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,sessionpageContext为null,application有值,关闭此浏览器再访问p3.jsp,request,session,pageContext为null,application

有值。如果在p3.jsp中

This is my p3 page. <br>

后添加代码

pageContext.setAttribute("scope","pagecomtext3");

访问p3.jsp时,application会有值。


从小到大范围:

  1. pageContext页面,只有在同一个页面内才会相同,尽管同一用户不同页面都不行。
  2. request请求,同一个用户,值要进行传递才相同,没有进行传递,其他页面也不会获得相应的值;
  3. session回话,同一回话;
  4. application项目,同一项目下,不同用户都拥有它,共享空间,并且值相同;


这是我学习时的理解,不知道好不好用,如有错误还请纠正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值