This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi, I have 2 java programs(one is a servlet and the other is a class file).The class file runs as an application when the main method is used. Now I want to call the methods in this class file from the servlet.The servlet is in a folder by name "controller" and the other class file is in a folder called "model". I want to call the initDatabase() method from the init() method in the servlet and the validateUser() method in the doPost() method. Kindly let me know what can be done.Any help would be gr8tly appreciated. Let me know if the codes for the respective programs are required. Thanks as
Billybob Marshall
Ranch Hand
Joined: Jan 27, 2004
Posts: 202
posted
0
Calling this class from the servlet is no different than calling it from any other java class. What's the beef?
nash avin
Ranch Hand
Joined: Nov 13, 2003
Posts: 96
posted
0
Thanks for the reply.But I am getting a whole bunch of cannot resolve symbols. The code for the servlet is posted below
PS: The UserValidator1 is the class that has the methods that have to be called. The errors I am getting are as follows
ControllerServlet1.java:15: cannot resolve symbol symbol : variable UserValidator1 location: class ControllerServlet1 UserValidator1.initDatabase(); ^ ControllerServlet1.java:33: cannot resolve symbol symbol : class UserValidator1 location: class ControllerServlet1 UserValidator1 uv = null; ^ ControllerServlet1.java:47: cannot resolve symbol symbol : class UserValidator1 location: class ControllerServlet1 uv = new UserValidator1(); ^ ControllerServlet1.java:48: cannot resolve symbol symbol : variable UserValidator1 location: class ControllerServlet1 boolean isValid = UserValidator1.validateUser(connection, user, pass);
Brian R. Wainwright
Ranch Hand
Joined: Aug 12, 2003
Posts: 92
posted
0
You need to import the UserValidator1 class into your servlet. Make sure it's in the classpath for your servlet container as well: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.sql.*; //import UserValidator1 import UserValidator1 Luck. --BW
Ravikumar Jambunathan
Ranch Hand
Joined: Aug 25, 2003
Posts: 66
posted
0
hi nash
Now I want to call the methods in this class file from the servlet.The servlet is in a folder by name "controller" and the other class file is in a folder called "model".
put the servlet and the class file in a package. Make sure the folder name and the package name are similar. Try to instantiate using packagename.classname() format. hope this will work. regards, ravi
nash avin
Ranch Hand
Joined: Nov 13, 2003
Posts: 96
posted
0
Hi, Thanks people for your thoughts.I am a little late in replying.Will check out your point of view. Will keep posting. Thanks AS
nash avin
Ranch Hand
Joined: Nov 13, 2003
Posts: 96
posted
0
Hi,te get you.Kindly let me know in as much detail as you can. I hope the responder to this topic previously will see this message.You have written that the folder name and the package name must be the same.I didnt quite understand. Put the servlet class and the other class in the same package,what does this mean. Let me know.Hope to hear from you. Thanks in adavance AS
Nash, Suppose, the first statement in your .java file says:
Then the class should be in a directory structure of: ../com/javaranch/code/MyClass.java If two classes are in the same package, they have the same package statement and are in the same folder. If you do not have package statements, all of your classes are in the default package. Some servers don't support the default package well.
Hi Nash, you can put the servlet and your java class in the same package or you put them in their own seperate package. For example if you decide to put them in the same package follow Jeanne's example above. But, based on your earlier statements
The servlet is in a folder by name "controller" and the other class file is in a folder called "model".
You should have a directory structure below: /controller/ControllerServlet1.java /model/UserValidator1.java You must have the below statement in each program respectively:
These packages should be placed in your WAR(web archive) and then placed in your servlet/jsp container. The structure should be similar to below: /WEB-INF/classes/controller/ControllerServlet1.class /WEB-INF/classes/model/UserValidator1.class I hope I got everything.
Craig
nash avin
Ranch Hand
Joined: Nov 13, 2003
Posts: 96
posted
0
Hi people, Thanks for your time in explaining in as much detail as possible.As Indicated I have put the necessary files in the necessary folders. I want to know whether I have set the class path correctly.Kidly guide me in this regard I have set my classpath as follows E:/WEB-INF/sourcefiles The controller and the model directories are present in the sourcefiles directory. Hope to hear a reply as early as possible. Thanks in advance AS