您的位置:首页 > 产品设计 > UI/UE

request.setAttribute与jsp:useBean 的区别

2007-07-12 16:39 567 查看
有点烟瘾 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






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)






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.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: