request.setAttribute与jsp:useBean 的区别

有点烟瘾 16:12:14
今天突然发现<jsp:useBean>与scope.setAttribute()及scope.getAttribute()有区别~有没有谁有兴趣交流一下?
 LanSky 16:12:54
说来听听,这几天我在补补JSP技术
   有点烟瘾 16:15:33
比如在一个页面中运行
request.setAttribute("bean",bean);
System.out.println(request.getAttribute("bean"));
会发现,不断刷新这个页面的时候,每次得到的bean都是一个新创建的对象
   有点烟瘾 16:16:01
实验结果表明,使用<jsp:useBean>的时候,也是创建了新的bean
   有点烟瘾 16:17:09
但是差别在于:假如bean中有一个属性为id,我将它的getId方法改为
public int getId() {
id++;
return id;
}
   有点烟瘾 16:18:21
使用request.getAttribute和request.setAttribute方法时,得到的id在刷新页面后是不会变化的,这正好与前面的实验结果——每次得到的bean都是一个新创建的对象,相吻合
   有点烟瘾 16:18:45
但是使用<jsp:useBean>的时候,id的值是每次加1的~
   有点烟瘾 16:19:27
有兴趣的可以实验一下~ 
 LanSky 16:19:42

 小兵seawave 16:21:13
恕我愚昧,烟瘾,测试结果表达什么?
   有点烟瘾 16:22:24
我一直以为jsp:useBean与scope.getAttribute及scope.setAttribute()方法一样,今天我专门做了上面的测试,证明不一样~

---------------------------------------------------------------------------

doublek321
Posts:164
Registered: 1/23/04
useBean vs (scope).setAttribute   
Jun 3, 2004 7:45 AM

Click to email this message

 
Is there any difference between saying these 2 things...

CASE 1
<jsp:useBean id="myFoo" scope="session" class="whatever.Foo"/>


...and this...

CASE 2
import whatever.Foo;
 
Foo myFoo = new Foo();
session.setAttribute(myFoo);


I realize that when another bean wants access to myFoo they're going to access it differently (either copy the statement listed in case 1 or do a session.getAttribute(myFoo) in case 2) but is that the only difference? Is there any particular reason to use one case over the other???



 
akulinsk
Posts:408
Registered: 22/01/99
Re: useBean vs (scope).setAttribute   
Jun 6, 2004 11:21 PM (reply 1 of 1)

Click to email this message

 
The reason is:

JSP's are intended to be created and maintained by web designers without knowledge of Java language. So servlet is creating some bean and puts it into session it is passed to JSP and then used by designer by using special tags.

If you really like to investigate if there is a different take a look at servelt source generated for JSPs created using both ways.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值