Problem compiling HelloWorld servlet

Hello,
I'm trying to compile a "Hello World" servlet in Tomcat that I copied straight out of the book. The code is as follows:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
However, neither HttpServlet, HttpServletRequest, HttpServletResponse, or ServletException are being recognized. In each case, the message I'm getting from the compiler is "cannot resolve symbol." I'm finding this whole situation very distressing. If someone could help me out, I'd greatly appreciate it.
Thanks,
Adam

well i have tried out both Servlets and JSPs in Windows 98 and i did not get the problem you have mentioned, i am using Jakarta Tomcat 3.2.1, and i have set the classpath to servlet.jar, actually this file is present within the jakarta-tomcat-3.2.1\lib directory and i suppose the classpath is automatically set as u run the startup.bat file. What you can try is, while setting the TOMCAT_HOME and ANT_HOME in the autoexec.bat, try doing it using the old dos 8.3 file naming style, for eg.
set JAVA_HOME=C:\jdk1.3
set TOMCAT_HOME=c:\jakart~1.1
set ANT_HOME=c:\jakart~1.1
then try running the example again.

Similar Messages

  • Problems compiling a servlet

    Hello,
    I recently installed Tomcat 4.1.24 and I am trying to compile a servlet but with no success. The error I get is package javax.servlet does not exist. I have set up jdk1.3.1-3 and the environment paths. I have been successful in compiling regular java applications. I am currently just using the dos prompt and command line c:\javac somefile.java
    When I tried to compile a servlet, I ran into the javax.servlet error so I found a javax.jar file and added it to the jdk1/3/1-3\lib directory but the compile still fails.. I even tried to add it to the environment path but that still doesn't work...
    could someone tell me where I would need to add the javax.jar file? and what adjustment I need to make to be able to compile servlets?
    thanks in advance for any help
    gjcat777

    You need to get J2EE (Java 2 Enterprise Edition). Download it and install it. It's basically a couple of JAR files that you need to use. Under $J2EE_HOME/lib there is a file called J2EE.jar Include that JAR file into your project and carry on with your servlet development.

  • Problem compiling simple servlet

    I am trying to compile a very simple servlet.
    When I do, I get the following error message:
    Package javax.servlet does not exist
    I am using the Java 2 SDK, Standard Edition Version 1.3.1.
    I have downloaded the servlet api (zip file servletapi2_1_1win.zip)
    from Sun's site. I unzipped these files in my lib directory; there are two folders,
    one called api and the other called src. Api and Src directories have their own
    sub-folders, javax/servlet/http etc.
    I am using W2K ; I had to create a CLASSPATH system variable in the environment
    variables section of control panel since there was none defined.
    I added the following line:
    C:\jdk1.3.1_01\lib\api\javax\servlet;C:\jdk1.3.1_01\lib\api\javax\servlet\http;
    I also did this for the PATH statement..
    My servlet looks like this:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class howdy extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello World </TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<BIG>Hello World </BIG>");
    out.println("</BODY></HTML>");

    Thanks. I was able to figure it out by specifying the
    full classpath
    at the command line, like this:
    C:> javac -classpath
    C:\jdk1.3.1_01\lib\servletapi2_1_1-win.zip\MyServlet.ja
    a
    Apparantly the system doesn't acknowledge the
    environmental variable that
    I created in the control panel; also I have no
    autoexec.bat file...so it
    looks like I have to write out the full path whenever
    I need to compile
    something. Kind of bothersome, but at least it
    works.
    You shouldn't have to do this. What operating system are you using?
    If you are using win2000, winNT or winxp, you should set the classpath in your system environment varialbes. You can also make a config.bat file and put everything in there. Then just run this config.bat file to set up your environment once for every session.
    Here's a file that I have set up on one of our NT machines. I just double click the icon for this file on my shortcut, and it gives me the window where I can compile my java programs.
    Hope this helps.
    Kamran
    =========== File SetJava.bat ============
    set PATH=%PATH%;m:\jdk1.3\bin
    set CLASSPATH=m:\jdk1.3\jre\lib;m:\jdk1.3\jaxp-1.1\jaxp.jar;m:\jdk1.3\jaxp-1.1\crimson.jar;m:\jdk1.3\jaxp-1.1\xalan.jar;.
    cd cis235
    %SystemRoot%\system32\cmd.exe

  • Problems Compiling javax.servlet and javax.servlet.http

    When I try to compile a program that imports both javax.servlet and javax.servlet.http, the compiler keeps telling me that "package javax.servlet does not exist"... I have place the javax.servlet packages in the src.zip and in the src folder but i dont understand why the compiler isnt picking them up. Maybe I did something wrong ... could someone help please

    Try to compile vith the option "-classpath /your/path/to/servlet.jar"

  • Having problems compiling *.java with import javax.servlet.jsp......

    I've been trying to do the tutorials in a book titled Apache Jakarta-Tomcat as part of my introduction to JSP. I'm new to Java but do know the basics. I have on my machine Java 1.3, Java 1.4, Java FrameWorks 2.1 and Jython 2.1 on Win2000 Pro. So enough about my configuration. I have never worked with packages also.
    I downloaded the files from the books site due to the fact I make a lot of typos when hand coding Java (I've become a pretty good debugger of my own code). But everytime I go to compile the *.java I get error messages. So below you will find HelloTag.java and beneath that the error messages that are thrown when I try and compile it. I tested other bits of *.java and have had no problem compiling them. Is there a JSP module I'm missing?
    Am I doing something wrong or am I missing something from my configuration.
    Marijan Madunic
    package chapter2;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspTagException;
    import javax.servlet.jsp.tagext.TagSupport;
    public class HelloTag extends TagSupport
    public void HelloTag() {
    // Method called when the closing hello tag is encountered
    public int doEndTag() throws JspException {
    try {
    // We use the pageContext to get a Writer
    // We then print the text string Hello
    pageContext.getOut().print("Hello");
    catch (Exception e) {
    throw new JspTagException(e.getMessage());
    // We want to return SKIP_BODY because this Tag does not support
    // a Tag Body
    return SKIP_BODY;
    public void release() {
    // Call the parent's release to release any resources
    // used by the parent tag.
    // This is just good practice for when you start creating
    // hierarchies of tags.
    super.release();
    D:\Java\JDK 1.3\bin>javac HelloTag.java
    HelloTag.java:3: cannot resolve symbol
    symbol : class JspException
    location: package jsp
    import javax.servlet.jsp.JspException;
    ^
    HelloTag.java:4: cannot resolve symbol
    symbol : class JspTagException
    location: package jsp
    import javax.servlet.jsp.JspTagException;
    ^
    HelloTag.java:5: cannot resolve symbol
    symbol : class TagSupport
    location: package tagext
    import javax.servlet.jsp.tagext.TagSupport;
    ^
    HelloTag.java:7: cannot resolve symbol
    symbol : class TagSupport
    location: class chapter2.HelloTag
    public class HelloTag extends TagSupport
    ^
    HelloTag.java:14: cannot resolve symbol
    symbol : class JspException
    location: class chapter2.HelloTag
    public int doEndTag() throws JspException {
    ^
    HelloTag.java:20: cannot resolve symbol
    symbol : variable pageContext
    location: class chapter2.HelloTag
    pageContext.getOut().print("Hello");
    ^
    HelloTag.java:24: cannot resolve symbol
    symbol : class JspTagException
    location: class chapter2.HelloTag
    throw new JspTagException(e.getMessage());
    ^
    HelloTag.java:28: cannot resolve symbol
    symbol : variable SKIP_BODY
    location: class chapter2.HelloTag
    return SKIP_BODY;
    ^
    HelloTag.java:37: cannot resolve symbol
    symbol : variable super
    location: class chapter2.HelloTag
    super.release();
    ^
    9 errors

    Well, it looks like you've not got the servlet development kit JAR on your classpath. I think it'll probably be called servlet.jar on your system. Add that to the classpath if it's not already there.
    Incidentally, you've bumped into one of the areas Java's slightly lenient - declaring a method that looks like a constructor:
    public HelloTag() { }
    public void HelloTag() { }The first is a constructor, the second is a normal method. I'm assuming you intended to have the former instead of the latter.
    Anyway, hope this helps.

  • Create problem when compiling the servlet

    hi,
    i got a problem when i compile a servlet
    when i try to compile the servlet, it shows that it could
    not found the classes like httpservlet, etc.
    means the classes of the servlet
    i am using the j2sdk1.4.1 edition,
    i had include the javax.servlet.*; in the program
    please tell me what can be the problem & especially any solution for that

    the javax.servlet package and it's sub packages (javax.servlet.http, javax.servlet.jsp, javax.servlet.jsp.tagext) are not included with the Standard Edition. These packages are part of the Enterprise Edition. Check out this link for download:
    http://java.sun.com/j2ee/sdk_1.3/

  • Problems to compile a servlet

    Hi, i have a problem to compile a servlet, i will try to explain my problem easily...
    i have a JavaBena class int he directory.....C:\Programmi\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes......and i have compiled it by DOS commands, everyhting has gone well!
    i have a Servlet class in the same directory where it situated the JavaBean. i compile the Servlet as well as i have done with the Javabean, by DOS commans, including the Tomcat servlet api.
    this is the command which i use to compile the servlet.......javac ServletRegistraUtente.java -classpath "C:\Programmi\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar".....
    but everything i get this error...
    C:\Programmi\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes>javac ServletRicercaUtente
    .java -classpath "C:\Programmi\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar"
    ServletRicercaUtente.java:20: cannot find symbol
    symbol : class BeanRicercaUtente
    location: class ServletRicercaUtente
    ^
    ServletRicercaUtente.java:20: cannot find symbol
    symbol : class BeanRicercaUtente
    location: class ServletRicercaUtente
    BeanRicercaUtente m_BeanRicercaUtente = new BeanRicercaUtente();
    ^
    2 errors
    C:\Programmi\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes>
    these are the 2 classes....
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;
    import JavaBean.BeanRicercaUtente;
    public class ServletRicercaUtente extends HttpServlet
         public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException
              ServletContext sc;
              RequestDispatcher rd;
              try
                   BeanRicercaUtente m_BeanRicercaUtente = new BeanRicercaUtente();
                   request.setAttribute("m_BeanRicercaUtente", m_BeanRicercaUtente);
                   String codiceFiscale = request.getParameter("codice");
                   m_BeanRicercaUtente.effettuaRicerca(codiceFiscale);
                   sc = getServletContext();
                   rd = sc.getRequestDispatcher("/risposta.jsp");
                   rd.forward(request, response);
              catch(Throwable theException){}
    import java.sql.*;
    public class BeanRicercaUtente
         String nome, cognome, indirizzo, telefono;
         public void effettuaRicerca(String codiceFiscale)
              try
                   Class.forName("com.mysql.jdbc.Driver");
                   String url = "jdbc:mysql://.................";
                   Connection con = DriverManager.getConnection(url);
                   Statement cmd = con.createStatement();
                   String qry = "SELECT ..................";
                   ResultSet res = cmd.executeQuery(qry);
                   while (res.next())
                        cognome = res.getString(1);
                        nome = res.getString(2);
                        indirizzo = res.getString(3);
                        telefono = res.getString(4);
                   res.close();
                   cmd.close();
                   con.close();
              catch (SQLException e)
                   e.printStackTrace();
              catch (ClassNotFoundException e)
                   e.printStackTrace();
    i would be glad to get help about it...! :) thanks!

    i am sorry, but in the first class i have not written "import JavaBean.BeanRicercaUtente;"
    i have only these imports...
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.io.*;

  • I have a problem compiling servlets bymeans of NetBeans IDE 3.6.

    If I compile a servlet by means of another IDE, they are oK and work well with NetBeans too.
    On the contrary, if I compile it bymeans of Netbeans 3.6, I get a .class which does not work. I
    have realized also that for instance if the .class i get with the other IDE is 1,58 KB, if I use
    NetBeans I get a .class smaller, that is 1,53 Kb.
    Can you help me?
    Thanks
    Eugenia Mariani

    ok
    This is the servlet named Contatore.java
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Contatore extends HttpServlet
    private int conta;
    public void init(ServletConfig config) throws ServletException
    conta = 0;
    public void doGet(HttpServletRequest richiesta, HttpServletResponse risposta)
    throws IOException, ServletException
    conta++;
    risposta.setContentType("text/html");
    PrintWriter out = risposta.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Pagina con contatore</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Questa pagina e' stata visualizzata");
    out.println("<h1>"+conta+ "</h1> volte.");
    out.println("</body>");
    out.println("</html>");
    out.close();
    Before using the compiler of NetBeans (that is after getting Contatore.class by means of Bluej) when I use Execute I see in Internet Explorer 6.0 with win Xp Pro at the
    http://localhost:8084/esempi/Contatore
    Questa pagina e' stata visualizzata
    1
    volte.
    and Contatore.class is 1,58 KB
    After that I use the compiler of NetBeans when I use Execute here the error from Tomcat 5.0
    I see in Internet Explorer 6.0 with win Xp Pro at the
    http://localhost:8084/esempi/Contatore
    HTTP Status 404 - Servlet Contatore is not available
    type Status report
    message Servlet Contatore is not available
    description The requested resource (Servlet Contatore is not available) is not available.
    Apache Tomcat/5.0.19
    and Contatore.class is 1,53 KB
    I hope to have said everything
    I wait a help, please.
    Thanks

  • Having problem running a servlet program in Tomcat5.5

    Hi,
    I wrote a sample servlet, HelloWorld.java and I compiled it and put the .class file in the WEB-INF\Classes directory in the Tomcat directory. Now when I do this,
    http://localhost:8080/ it's working fine but when I do this, http://localhost:8080/servlet/HelloWorld I am getting an error message "*HTTP Status 404"*.
    I have done no changes to web.xml and server.xml. Should I be doing anything?
    My HelloWorld.Java file is as below.
    package Users.Ravi.Desktop.CurrentProject.Servlets;
    import java.io.*; 
    import javax.servlet.*;  
    import javax.servlet.http.*;
    public class HelloWorld extends HttpServlet {
    public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
          throws ServletException, IOException { 
        PrintWriter out = response.getWriter();
        out.println("Hello World");
    }Thanks.

    Ah, I see the problem now. You need to put servlet class as
    <code class="jive-code jive-java">Users.Ravi.Desktop.CurrentProject.Servlets.HelloWorld</code>
    <code class="jive-code jive-java">So, your web.xml will have</code>
    <servlet>
         <servlet-name>HelloWorld</servlet-name>
         <servlet-class></code><code class="jive-code jive-java">Users.Ravi.Desktop.CurrentProject.Servlets.HelloWorld</code><code class="jive-code jive-java"></servlet-class>
    </servlet>{code}

  • A strange problem in Tomcat Servlet

    I am a starter in programming servlet.
    I am running servlet under Tomcat 5.5.
    I test the HelloWorld servlet. It works well and shows "Hello World".
    But when I change the code to out.println("Hello World!!!"), and re-compile, the output remain the same.
    Then I shut down the Tomcat and restart. The output becomes Hello World!!!.
    What's the reason? The servlet engine only read the same class once?
    How can I change it?
    Thanks

    With class files, I've noticed that sometimes changes to the code aren't displayed, you have to restart tomcat.
    But if you're just displaying text, that should go in a jsp. In a jsp, changes are usually displayed when you reload the page (unless the browser caches the page). And the code is also cleaner to look at, without out.println's everywhere.
    If you're using eclipse as your programming environment, then it should automatically do a "hot code replace" (which does fail in some cases, like if you add a method, so you have to be careful). If the changes are minor, you can do exactly what you're looking--modify a class, save it, and the class file gets changed without having to restart tomcat, and the recompilation is automatic which is great.

  • Compiling a Servlet that uses JavaBean

    I have a servlet that will get data from a JavaBean which is created before in a jsp file. While I trying to compile the servlet I am getting this message:
    --------------------Configuration: JDK version 1.3 <Default>--------------------
    Command : "C:\jdk1.3\bin\javac.exe" -d "C:\jakarta-tomcat-3.2.1\webapps\ROOT\WEB-INF\classes" C:\jakarta-tomcat-3.2.1\webapps\ROOT\WEB-INF\classes\MyServlet.java
    Directory : C:\jakarta-tomcat-3.2.1\webapps\ROOT\WEB-INF\classes
    C:\jakarta-tomcat-3.2.1\webapps\ROOT\WEB-INF\classes\TasitKayitGirisi.java:19: cannot resolve symbol
    symbol : class SimpleBean
    location: class MyPackage.SimpleBean
         SimpleBean simpleBean = (SimpleBean) session.getAttribute
    ^
    ("simpleBean");
    C:\jakarta-tomcat-3.2.1\webapps\ROOT\WEB-INF\classes\MyServlet.java:19: cannot resolve symbol
    symbol : class SimpleBean
    location: class MyPackage.SimpleBean
         SimpleBean simpleBean= (SimpleBean) session.getAttribute("simpleBean");
    ^
    2 errors
    Process completed.
    Where is the problem. SimpleBean is successfully being initializing in my jsp file. But I couldn't compile my servlet that will get data from that bean. Both my servlet and bean are in the same package. I am using JCreator to compile servlets. I think the problem is the compile string that JCreator uses. Can you help me. Thx in advance.

    Looks like an import Problem.
    The Compiler is not able to "see" the SimpleBean Class.
    Take care of the Classpath and your imports.
    Happy Coding. :-)

  • Help noob! HelloWorld Servlet

    Using Windows & Sun Java System Application Server Platform Edition 9.0_01
    I just would like to get such result:
    http://localhost:8080/HelloWorld?name=MYNAME
    Where HelloWorld is my simple servlet displays MYNAME
    HelloWorld.java:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Hello extends HttpServlet
         public void doGet(HttpServletRequest req, HttpServletResponse res)
              throws ServletException, IOException
         res.setContentType("text/html");
         PrintWriter out = res.getWriter();
         String name = req.getParameter("name");
         out.println("<HTML>");
         out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
         out.println("<BODY>");
         out.println("Hello, " + name);
         out.println("</BODY></HTML>");
    I have compiled it and get HelloWorld.class
    Then I've copied that calss file to autodeploy folder.
    Few minutes later I logged onto the Admin Page and saw that my Web Module appears in Applications -> Web Applications.
    I'm trying http://localhost:8080/HelloWorld?name=MYNAME
    "HTTP Status 404 - The requested resource () is not available."
    I'm trying http://localhost:8080/HelloWorld
    and getting just to /HelloWorld/ directory
    "Directory Listing For /"
    So, what am I doing wrong?
    How to access my servlet?

    jaikiran, thanks for youe reply
    I wish web.xml would be auto created after autodeploy..
    But it didn't.
    So I've created it myself and put to:
    \domains\domain1\applications\j2ee-modules\HelloWorld\WEB-INF
    <web-app>
    <display-name>heelo</display-name>
    <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
    </web-app>
    And I still getting the contents of direcory.... :(
    I've got a feeling that I'm not doing autodeploy correctly...
    Maybe there is some tutorial for hand-compiled classes wicha are being autodeployed? :oops:

  • Compiling Java Servlet with Depricated Methods

    I have a problem of compiling Java servlets that contain depricated methods. If you compile against modern version of servlet.jar I see depricated methods error messages and if I use a very old servlet.jar it cannot find newer methods. What is is the best solution in this case?
    Thank you,
    Boris.

    The best thing to do would be compile your code with the -depreciated option to find which methods have been depreciated and the refactor your code to remove them.
    The next best thing is to ignore the messages (they are warnings not errors) and your code should still work

  • Just can't run HELLOWORLD servlet on Tomcat!

    Hi there, I worked with servlets all summer long and now I'm getting back to build one, and I'm totally confused... My problem's there :
    I try to run a simple HelloWorld servlet on Tomcat 4.1, and I get a 404 Error - Not found
    Well, here's the structure of my web application, contained in simpleservlet.war :
    WEB-APP
    -web.xml
    -CLASSES
    -servlet.class
    -LIB
    -servlet.jar
    Here's now the content of the web.xml file :
    <web-app>
         <servlet>
              <servlet-name>serv</servlet-name>
              <servlet-class>servlet</servlet-class>
         </servlet>
         <servlet-mapping>
              <servlet-name>serv</servlet-name>
              <url-pattern>/test</url-pattern>
         </servlet-mapping>
    </web-app>
    AND FINALLY here's how I invoke my servlet, I TRIED :
    http://localhost:1977/servlet/test
    AND ALSO
    http://localhost:1977/test
    AND BANG, I GET "ERROR 404 - NOT FOUND"
    A little help would be appreciated A LOT, well it's not supposed to be a hard problem... and by the way, I'm using port :1977, the problem's not there though.
    regards, Pierre

    Hi,
    First try http://localhost:1977 and see if you are getting Tomcat. As you said, this is not a problem. So, you should get the index.html of the Tomcat. Now, click on the one of the example. From the Address Bar, you will know the path of this example. Try putting your page at that location and then run.
    This should work.
    -- Ashish.

  • How to Compile/Run servlet in Tomcat 4.1

    Hi All,
    iam trying to compile my servlet program in tomcat4.1.. but its showing an error
    in the import statement..
    UN SOLVABLE SYMBOL... but the import ststement is correct...
    can any one help ... is there i have to set any path for compiling/running the servlet...
    Thanks in Advice

    hi thanks for your reply
    i have loaded tomcat4.1 exe -- for windows i am starting the tomcat server by clicking from the start menu..
    i dint set any env variable .. tell me how to set...
    regards
    ijay

Maybe you are looking for