jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes help please as to how to go about doing this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "help please as to how to go about doing this" Watch "help please as to how to go about doing this" New topic
Author

help please as to how to go about doing this

nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
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
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
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
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
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
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
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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30370
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help please as to how to go about doing this