Link to home
Start Free TrialLog in
Avatar of foongkim2
foongkim2

asked on

Can JavaBean retrieve another JavaBean Property?

Hi, the question title is clear is it? I have a senario which I think I am out of ideas.

I would like to ask is it possible for JavaBean1 retrieve the JavaBean2's property??

JavaBean1.java
==============

private String var1 = "";
...
...
public void setVar1(v1) {
       var1=v1;
}
public String getVar1() {
   return var1;
}
...
...


JavaBean2.java
==============
....
import abc.javabean1
...
...
String pk = javabean1.getvar1();
..
..
..

Can it be done??
I think it's shouldn't be any problem. The only concern is the location of the bean, the import location. Let's say my JavaBean1 is store in abc/pk/JaveBean1.java and JavaBean2.java is in the same directory means abc/pk/JavaBean2.java, how would be my import statement look like??





Avatar of Mick Barry
Mick Barry
Flag of Australia image

Yes it can be done, but not by importing (though you need the import to use the class).
Importing is used to specify the classes used, not instances of classes.

You instead need to pass a reference of your bean to your other bean:

import abc.JavaBean1

public class JavaBean2
{

   private JavaBean1 javabean1;

   public void setJavaBean1(JavaBean1 bean)
   {
      javabean1 = bean;
   }

...
Avatar of foongkim2
foongkim2

ASKER

Object, confused.
Let's discuss throguht the code I posted just now.

javabean1.java
==============
....
package com.wrox; //location of my JavaBean1
...
...
public class javabean1 {

 private String var1="";
...
...
public void setVar1( Sting v1) {
     var1=v1;
}

public String getVar1() {
     return var1;
}
..
..
}

javabean2.java
==============
...
...
package com.wrox; // location of my javabean2
import com.wrox.javabean1; //Do i need this???
...
private javabean1 jb1;
...

public void setJb1 ( String j1 ) {
     jb1 = j1;
}
public String getJb1 (
     return jb1;
}
...
...


Did I code it right?? And the import com.wrox.javabean1 should be there?? ( I think it should )

The import does not import an object instances.
It just tells the compiler what class(s) you are using.

Your code is fine, but you need to somewhere set the value of jb1.
OK man.... I think I am improving alot....

Do u agree? Since you are the one who share with me most. ha...:-)
Yes you are improving :)
Objects, one extra things wish to ask you.

In the JavaBean declaration part,

<jsp:useBean id="abc" class="com.wrox.javabean1: scope="session" />


regarding the "scope" calue, according from the book, that can be set as page, request, session and application. For my senario, which is consists of 4 jsp pages, and 3 javabean, I would like to keep track of the previous data enter by the user. That's why I use this "scope" functiona. Yet, I found some problems.

1. is it when I implement this scope, will the server request more resources? ( i think it will because the JSP container or server need more procresssing power or memory to store the value of the jsp property).

2. If I want to use this scope, which one is the best to use? I have tried the session but I found the session is not tha stable. Because when I put my page idle for some time,it'll gone. ( I think because of the session time out problem).

3. Finally, do u encourage prople to use application, which mean the JSP property can be access all over the JSP application system in all the 4 JSP page and 2 JavaBea in my case.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Object, Thanks for your help.

I am still learning from you. Much much more to learn from you. Ha..

Thanks.
Object.... can not again....

"Can not resolve symbol"

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
...
private String billcode="";
...
}

stockamount.java
================

package com.wrox.cars;
import com.wrox.cars.debit1bean;

public class stockamount {
   private debit1bean d1;
...
...

Then the error pointing to stockamount.java in debit1bean, saying that cannot resolve symbol...

I am very sure that the debit1bean.java is exist in the directory... com.wrox.cars


 


Check that debit1bean.class exists in the directory... com.wrox.cars
It's there .......

Do u agree that anything wrong with the declaration of the coding inside the debit1bean.java???

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
..
...
}

It's there .......

Do u agree that anything wrong with the declaration of the coding inside the debit1bean.java???

debit1bean.java
===============
package com.wrox.cars;

public class debit1bean {
..
...
}

no that looks fine.