Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

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

Avatar of Rick_Townsend
Rick_Townsend

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.
Avatar of jaggernat

ASKER

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

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?
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.
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)
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,
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?
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.
yes , Apache  Tomcat 4.1 is on      and i am using the comand line Dos compiler

thanks,
J
sorry..by Dos compiler , i mean javac on dos prompt.
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
tried...same error ..cannot resolve symbol    ServletUtilities.
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.
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
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.
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


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
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
ok its  

Apache Tomcat/4.1.31

i did add .;  in the classpath.

Thanks for helping me out. I appreciate it.

J

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.
sure thanks
Dear administrator,

I want to delete this question

thanks,
J
ASKER CERTIFIED SOLUTION
Avatar of Rick_Townsend
Rick_Townsend

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it works now.
thanks,
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?