Solved

servlet Programming

Posted on 2004-10-27
1,023 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
Question by:jaggernat
    25 Comments
     
    LVL 4

    Expert Comment

    by: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.
    0
     
    LVL 10

    Author Comment

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

    Expert Comment

    by:Rick_Townsend
    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
    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
    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
    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
    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
    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
    sorry..by Dos compiler , i mean javac on dos prompt.
    0
     
    LVL 4

    Expert Comment

    by:Rick_Townsend
    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
    tried...same error ..cannot resolve symbol    ServletUtilities.
    0
     
    LVL 4

    Expert Comment

    by:Rick_Townsend
    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
    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
    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
    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
    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
    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
    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
    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
    sure thanks
    0
     
    LVL 10

    Author Comment

    by:jaggernat
    Dear administrator,

    I want to delete this question

    thanks,
    J
    0
     
    LVL 4

    Accepted Solution

    by:
    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
    it works now.
    thanks,
    0
     
    LVL 4

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Hi, I've made you some graphics for a better understanding how RAID works. First of all, there are two ways a raid can be generated: - By hardware - By software What does that mean? This means: If you have a hardware RAID controller, there…
    The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
    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 Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now