[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

servlet Programming

Posted on 2004-10-27
25
Medium Priority
?
1,058 Views
Last Modified: 2010-04-03
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

0
Comment
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 10
25 Comments
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12438500
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12438585
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12438732
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12438744
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12438757
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12438868
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12439422
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12439436
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12439646
yes , Apache  Tomcat 4.1 is on      and i am using the comand line Dos compiler

thanks,
J
0
 
LVL 10

Author Comment

by:jaggernat
ID: 12439996
sorry..by Dos compiler , i mean javac on dos prompt.
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12440647
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12441026
tried...same error ..cannot resolve symbol    ServletUtilities.
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12441064
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12441092
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12441098
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12446476
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12452788
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
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12452836
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12453383
ok its  

Apache Tomcat/4.1.31

i did add .;  in the classpath.

Thanks for helping me out. I appreciate it.

J

0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12454269
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12455504
sure thanks
0
 
LVL 10

Author Comment

by:jaggernat
ID: 12479362
Dear administrator,

I want to delete this question

thanks,
J
0
 
LVL 4

Accepted Solution

by:
Rick_Townsend earned 2000 total points
ID: 12487763
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12507870
it works now.
thanks,
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12514552
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question