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()方法一样,今天我专门做了上面的测试,证明不一样~
---------------------------------------------------------------------------
今天突然发现<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. |
相关文章推荐
- jsp内置对象request中getParameter方法与get/setAttribute方法的区别
- JSP中request.setAttribute()和session.setAttribute()两个方法的区别
- request.setAttribute和session.setAttribute有什么区别
- request.getParameter()与request.setAttribute()的区别
- (转)request.getParameter()与request.setAttribute()的区别
- request.getParameter()与request.setAttribute()的区别
- jsp中request.getParameter和request.getAttribute之间的区别
- request.setAttribute和request.getSession().setAttribute的区别
- java 中的request.setAttribute和session.setAttribute的区别
- request.getParameter()与request.setAttribute()的区别
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
- request.setAttribute()、session.setAttribute()和request.getParameter()、request.getAttribute()的联系与区别
- jsp request.getAttribute()与request.getParameter()的区别
- request.getParameter()与request.setAttribute()的区别
- request.setAttribute()和request.getSession().setAttribute()的区别
- jsp中request.getParameter和request.getAttribute之间的区别
- jsp中的request.setAttribute的使用
- request.setAttribute()和request.getSession().setAttribute()区别
- request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别
- request.getParameter()与request.setAttribute()的区别