This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help please as to how to go about doing this

 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic