Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Bean Scope Attribute

 
meng zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone,
I want to share a bean instance data across pages.
I create two jsp pages, one is setUserInfo.jsp, to set the bean property, another is getUserInfo.jsp to get bean peroerty. I set a link in the setUserInfo.jsp, to refer to the getUserInfo.jsp.

However, I can not see the propery value in the getUserInfor.jsp page. I thought scope attribute might determine the visibility range of the bean, In both jsp pages, I set scope attribute to "session" and "applicaiton" , but neither works.
Could you tell what I am wrong?

Folloing is the my code and jsp. Thanks,

1. setUserInfor.jsp
********************************************************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Java Session Bean Example</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="user" class="moreservlets.User" scope="application" />
<jsp:setProperty
name="user"
property="name"
value="martin" />

<jsp:setProperty
name="user"
property="addr"
value="2442 7ave NW" />

This jsp page is feeding the java session bean user with the following info.
<br>
name: martin
<br>
addr: 2442 7ave nw
<br>
<a href="getUserInfo.jsp">
click to </a> go to the getUserInfo.jsp for user information
</p>

</BODY>
</HTML>
***********************************************************************
2. getUserInfor.jsp
***********************************************************************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Java Session Bean Example</TITLE>
</HEAD>
<BODY>
<jsp:useBean id="user" class="moreservlets.User" scope="application" />
User Information:
<br>
name: <jsp:getProperty name="user" property="name" />
<br>
addr: <jsp:getProperty name="user" property="addr" />


</BODY>
</HTML>
**********************************************************************
3.User.java
**********************************************************************
package moreservlets;
public class User {

private String name;

private String addr;


public void setName(String name) {

name=this.name;
}

public String getName() {

return name;
}


public void setAddr(String addr) {

addr=this.addr;
}

public String getAddr() {

return addr;
}

}
************************************************************************
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JSP is all working fine. The problem is in your bean class. You're not correctly setting the instance variables:

When your object is first created, the instance variables are set to null. When you call setXXX(String), all your code is doing is setting the parameter to null.
It should read something like this:

After that you should be fine. And of course, you probably want to set your scope for the "useBean" to something more restrictive than application once you find that this will work.
[ March 24, 2004: Message edited by: Nathaniel Stoddard ]
 
meng zhou
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Nathaniel Stodard.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic