This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Java Bean Scope Attribute

 
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;
}

}
************************************************************************
 
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.
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!