• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean doubt

 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package test;
public class MyBean
{
private String name = "default";
public MyBean(){}
public MyBean(String name){this.name=name;}
public void setName(String name){ this.name = name; }
public String getName(){ return name; }
}



myjsp.jsp
<%
pageContext.setAttribute("bean", new test.MyBean("no name"));
%>

<jsp:useBean id="bean" class="test.MyBean">
<%bean.setName("bean");%>
</jsp:useBean>

Name:<%= bean.getName()%>

when i set "bean" attribute with pageContext my output is "Name:no name"
when i set "bean" attribute with request/application/session my output is
"Name:bean"

what is happening behind the scene can anyone explain in detail?

thanks inadvance
Sravanthi
[ May 30, 2008: Message edited by: Vijaya Sravanthi ]
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name:<%= account.getName()%>

what is account and where did you declare it?
 
Bhupendra Khabrani
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i set "bean" attribute with pageContext my output is "Name:no name"


when you set bean in the pageContext the default scope of jsp:useBean already has the bean(as you haven't given the scope attribute!!) so the body of useBean std action is not executed hence you get the value which you had set while creating the bean.

when i set "bean" attribute with request/application/session my output is "Name:bean"


while when you set it in request/ session/ application scopes useBean tries to find it in page scope (as thats the default) which it doesn't find hence standard actions' body is executed hence creating a new bean in the page scope..confused...!!
the bean already there in the other scope will remain there and a new bean for the page scope will be created by the body of useBean action.

thanks and reagards
Bhupen
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic