servlet Programming

hi i have this simple question on servlets

I have this servlet program called ServletUtilities.java :
--------------------------------------------------------------

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUtilities
{
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \" - // W3C// DTD HTML 4.0"+"Transitional // EN\">";
public static String headWithTitle(String title)
      {
      return(DOCTYPE + "\n" + "<HTML>\n" +"<HEAD><TITLE>" +title+ "</title></head>\n");
      }
}

--------------------------------------------------------
NOW HERE  IS MY MAIN  SERVLET PROGRAM called Hellowww3.java which calls the ABOVE PROGRAM ServletUtilites.java.....
--------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hellowww3 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("Text/Html");
PrintWriter out = response.getWriter();

//this line calls "headWithTitle" method of "ServletUtilities" java class described above.

out.println(ServletUtilities.headWithTitle("This is the title HelloWWW") +
"<body>\n" +
"<h1>HELLO WWW</h1>\n"+
"</body></html>");
}
}
--------------------------------------------------------------

For some reason , the Hellowww3 program shows an error while compiling although both the programs are placed in the same folder.
When compiled Hellowww3.java , it says "cannot resolve symbol :ServletUtilities...)

any help greatly appreciated,

thanks,
J

LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rick_TownsendCommented:
This should probably be in the Programming > Languages > Java channel, but I'll try to help you.

I dropped the source for those two files into a project, and they compiled just fine for me.  Did ServletUtilities.java compile properly for you?  If it did not compile, you probably don't have the javax.* packages in your Java installation.  They don't come with the standard j2sdk.
0
jaggernatAuthor Commented:
sorry for pasting this question in this location.
Regarding the above problem

ServletUtilities.java compiled just fine ,,,but the Hellowww3.java does not ..It cannot figure out this line
out.println(ServletUtilities.headWithTitle("This is the title HelloWWW") + "<body>\n" + "<h1>HELLO WWW</h1>\n"+
"</body></html>");



J

0
Rick_TownsendCommented:
There are a few more things we can check.

Is the class file (ServletUtilities.class) file located in the same directory as your source file (Hellowww3.java)?  That is, when you compiled ServletUtilities.java, did you move the class file that was generated?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rick_TownsendCommented:
Also, check the spelling on the class file.  It should be fine, since the name of the class is spelled correctly in both of your comments, and the source won't compile if the filename doesn't match the class, but it's worth checking.
0
Rick_TownsendCommented:
If the above two don't help you, rename the ServletUtilities.class file to ServletUtilities.old, and then try to compile both source files (.java files) at the same time.

(if you are using an IDE, this will probably happen automatically.  If you are using the command line, go to the directory where your source files are located, and run a full compile:  javac *.java)
0
jaggernatAuthor Commented:
no it doesnt work. when i did javac *.java ,it spit 32 errors..

The problem here is very clear Hellowww3.java cannot recognize the ServletUtilities.java program ,although ,both

 the programs and their respective class files are stored in the same direcotry that is  c:\program~1\Apache~1\Tomcat 4.1\webapps\examples\web-inf\classes.

may be this location is the problem or some path informatio is missing.

thanks,
0
Rick_TownsendCommented:
Yes, the problem was obvious, however the root cause of the problem isn't.  The error you're seeing is thrown when the compiler can't find the class file for the class you're referencing.  The reason it can't find the file is what we have to work out.

Are you using and IDE to compile your classes, or just the command-line compiler?
0
Rick_TownsendCommented:
Also, are you able to run Tomcat?  If you can't run Tomcat, then you are likely missing environment variables that you have to set up after installing Tomcat.  Read the file RUNNING.txt in your Tomcat home directory.
0
jaggernatAuthor Commented:
yes , Apache  Tomcat 4.1 is on      and i am using the comand line Dos compiler

thanks,
J
0
jaggernatAuthor Commented:
sorry..by Dos compiler , i mean javac on dos prompt.
0
Rick_TownsendCommented:
Well, you can force the compiler to look in the current directory.

Use the following:
javac -classpath %classpath%;. Hellowww3.java

If the compiler starts complaining about javax packages, use the following:
javac -classpath c:\program~1\Apache~1\Tomcat 4.1\common\lib\servlet-api.jar;. Hellowww3.java
0
jaggernatAuthor Commented:
tried...same error ..cannot resolve symbol    ServletUtilities.
0
Rick_TownsendCommented:
Then your class must be mis-named.  Two more tries:

javac -cp . Hellowww3.java

or

javac -cp .\ServletUtilities.class


The cp switch is the same as classpath, but I have only included the current directory (ie: ".") in the first one.  If that doesn't work, and ServletUtilities.class is in the same directory, then the ServletUtilities.java file isn't compiled properly.
0
Rick_TownsendCommented:
I was able to successfully compile both files by copying them into my <tomcat-home>/webapps/servlets-examples/WEB-INF/classes/  directory, and then running the following:

javac -cp <tomcat-home>\common\lib\servlet-api.jar ServletUtilities.java

javac -cp <tomcat-home>\common\lib\servlet-api.jar;. Hellowww3.java
0
Rick_TownsendCommented:
If none of the four compile lines in the above comments work for you, then tell me you java and tomcat versions, and I'll mirror your environment on my machine.
0
jaggernatAuthor Commented:
its j2sdk1.4.2_06 and Tomcat 4.1

ServletUtilities.java  (stored in c:\progra~1\Apache~1\Tomcat 4.1\webapp\examples\WEB-INF\classes)
------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUtilities
{
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \" - // W3C// DTD HTML 4.0"+"Transitional // EN\">";
public static String headWithTitle(String title)
      {
      return(DOCTYPE + "\n" + "<HTML>\n" +"<HEAD><TITLE>" +title+ "</title></head>\n");
      }
}

-----------------------------------------------------------------
Hellowww3.java (stored in c:\progra~1\Apache~1\Tomcat 4.1\webapp\examples\WEB-INF\classes)


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hellowww3 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("Text/Html");
PrintWriter out = response.getWriter();
out.println(ServletUtilities.headWithTitle("This is the title HelloWWW") + "<body>\n" + "<h1>HELLO WWW</h1>\n"+
"</body></html>");
}
}
----------------------------------------------------
CATALINA_HOME=set CATALINA_HOME=c:\Program Files\Apache Group\Tomcat 4.1
classpath= C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\Program Files\Apache Group\Tomcat 4.1\Common\lib\servlet.jar
path=%SystemRoot%\system32;%SystemRoot%;C:\Program Files\Mts;set PATH = %PATH%;C:\j2sdk1.4.2_06\bin;%JAVA_HOME%\bin;%CATALINA_HOME%\bin"
JAVA_HOME= set JAVA_HOME=C:\j2sdk1.4.2_06


thanks,
Jay


0
Rick_TownsendCommented:
I see a couple of problems with your environment variables.  I'm surprised that Tomcat will run with them set like that.  Are you setting them from the command prompt, or through the Windows Environment Variable manager?  (Right-click on My Computer, choose properties, select the Advanced tab, and click Environment Variables).

Normally, your CATALINA_HOME and JAVA_HOME variables should look like this:

CATALINA_HOME=c:\Program Files\Apache Group\Tomcat 4.1
JAVA_HOME=C:\j2sdk1.4.2_06
0
Rick_TownsendCommented:
Apache never released a version of Tomcat numbered "4.1" or "4.1.0", so I'm assuming that you have one of the other "4.1.x" releases.  Start up Tomcat, then go to the Tomcat welcome page (usually http://localhost:8080 ).  At the top left of the screen will be "Apache Tomcat/4.1.something", where "something" is the build number.  Please post that.

I've set up an environment with Tomcat 4.1.31 (the latest build in the 4.1.x stream), and j2sdk 1.4.2_05.  I'll see about downloading 1.4.2_06 once I know the exact Tomcat build you're using.


As a temporary measure, please add ".;" at the start of your classpath.  Your classpath would then look like this:

classpath=.;C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\Program Files\Apache Group\Tomcat 4.1\Common\lib\servlet.jar


Then open a new command prompt, and echo the classpath variable:

echo %classpath%


You should see:

.;C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\Program Files\Apache Group\Tomcat 4.1\Common\lib\servlet.jar
0
jaggernatAuthor Commented:
ok its  

Apache Tomcat/4.1.31

i did add .;  in the classpath.

Thanks for helping me out. I appreciate it.

J

0
Rick_TownsendCommented:
You're welcome.  Since you haven't closed the question, I assume that adding the local directory to the classpath didn't help.

I'll download j2sdk 1.4.2_06, and modify my classpaths to match yours.  We'll figure this out.
0
jaggernatAuthor Commented:
sure thanks
0
jaggernatAuthor Commented:
Dear administrator,

I want to delete this question

thanks,
J
0
Rick_TownsendCommented:
Hey J,

Sorry I haven't been around.  My wife has been in the hospital since Sunday, so I haven't bothered checking my email.


I set up a test environment using Tomcat 4.1.31 and j2sdk1.4.2_06.  I created the two java files as you specified:

C:\Program Files\Apache Group\Tomcat 4.1\webapps\examples\WEB-INF\classes\ServletUtilities.java
and
C:\Program Files\Apache Group\Tomcat 4.1\webapps\examples\WEB-INF\classes\Hellowww3.java


I set my classpath:
set classpath=C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\Program Files\Apache Group\Tomcat 4.1\Common\lib\servlet.jar;.
(which is the classpath you specified, except that I added ";." at the end)


I successfully compiled ServletUtilities.java:
javac ServletUtilities.java


I successfully compiled Hellowww3.java:
javac Hellowww3.java


I therefore fall back to my previous suggestion, that either ServletUtilities was not compiled correctly, or your classpath is not specified correctly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaggernatAuthor Commented:
it works now.
thanks,
0
Rick_TownsendCommented:
Glad it's working.  For the sake of anyone else with the same problem who might read this thread, do you know what the problem was and/or what you did to fix it?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Storage

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.