Actually, my task is to make java bean classes inside a same package and use them into JSP (java server pages).
Below is what i am doing.
Package name: mypack
*1st class name: UserBean.java* (contains all getters and setters methods)
2nd class name: Databean.java (contains the methods to interact with database)
d:\java: jdk1.5 and tomcat, both are in this folder.
UserBean.java: I want to use the setter and getter methods of this class to set and get the values.
DataBean.java: I want to use this class’s methods to interact with MS Access database like fetch, update, delete etc. For these actions, i want to instantiate UserBean class object into DataBean Class to set and get the values of DataBean class.
How i am compiling?
d:\java\mypack> set path=d:\java\jdk1.5\bin
d:\java\mypack>javac UserBean.java
But when i instantiate UserBean class’s object inside the DataBean Class, i get the error that i have already mentioned.
d:\java\mypack>javac DataBean.java
here i get that above error message
to resolve this issue, i modified the compiling line a little bit:
d:\java\mypack>javac -classpath .. DataBean.java
this works and successfully generates the class file
For now, both classes are being compiled successfully. There is another issue now and is described below.
package mypack;
**//UserBean.java**
public class UserBean{
String username; //an attribute of UserBean Class
String password; //an attribute of UserBean Class
public UserBean(){} //Default Constructor of UserBean Class
public void setUsername(String username){ this.username = username; }//setter method
public String getUsername(){return username; } //getter method
}
package mypack;
**//DataBean.java**
public class DataBean{
UserBean user = new UserBean(); //Instantiating UserBean class object in
//DataBean class
String username; //an attribute of DataBean Class
public DataBean(){} //Default Constructor of UserBean Class
public String getUsernameTwo(){return user.getUsername(); } //gettter method
}
On jsp page, i instantiated two objects
<%@ page import="java.sql.*, mypack.*"%> //importing packages
<html>
<body>
<%
UserBean u = new UserBean(); *//UserBean class object u has been instantiated*
DataBean d = new DataBean(); //DataBean class object d has been instantiated
u.setUsername("John"); //username has been set as john
out.println(u.getUsername()); //gets the string john, and prints it successfully
(prints: John)
out.println(d.getUsernameTwo()); //Does'nt print John, but prints null
//**(it should print john too but it doesnt)**
%>
</body>
</html>
The method getUsernameTwo() returns the user name of a UserBean that has been created in the constructor of DataBean. This UserBean instance is not the same UserBean object as the one that you instantiated in your JSP.
That’s like opening two bottles, putting a message in one of the bottles, and looking inside the other bottle hoping to find the message you put in the first one. It doesn’t work with bottles, and doesn’t work with Java objects either, because Java objects are somewhat like real objects.
One thing you could do is to make the DataBean use a UserBean passed in its cnstructor:
And in your JSP:
It seems you haven’t grasped basic OO concepts yet. I would advise starting tutorials on basic Java and OO first, before playing with JSPs.