jQuery in Action, 2nd edition*
The moose likes JSP and the fly likes Java Bean Scope Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Java Bean Scope Attribute" Watch "Java Bean Scope Attribute" New topic
Author

Java Bean Scope Attribute

meng zhou
Greenhorn

Joined: Jan 16, 2004
Posts: 12
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

Joined: May 29, 2003
Posts: 1258
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 ]

Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
meng zhou
Greenhorn

Joined: Jan 16, 2004
Posts: 12
Thanks, Nathaniel Stodard.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Bean Scope Attribute
 
Similar Threads
beans
jsp:getProperty calling bean from specific scope
Reason for change of scope from request to page
JasperException from non-numeric parameter value
redirect a bean?