1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Please help with Java Servlet problem

Discussion in 'Mac Programming' started by ddmcnair, Apr 26, 2011.

  1. macrumors newbie

    Please help with Java Servlet problem
    I have been trying for three days to get a simple Servlet to work. Tomcat is working and displaying the work. I have created the blasted web.xml, .java, and .class files. All I get is HTTP 404 /Hi/ or /Hello/ could not be found. I hate Tomcat, I hate Java Servlets. If anyone can help please, please, please, I beg you, please help. Here is the java file:

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

    public class Hi extends HttpServlet {

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
    PrintWriter out = response.getWriter();

    out.println("<body bgcolor=\"white\">");
    out.println("<h1> Hi </h1>");

    Here is the web.xml file:
    <display-name>My Web Application</display-name>



    I am using Mac, but for intents and purposes, for Tomcat, Unix.
    The files are posted in /usr/local/apache-tomcat-7.0.12/webapps/Hi. Any help would be greatly and most sincerely appreciated, PLEASE HELP. At this point I am desperate.
  2. Guest

    OK, back up one step. Do any of the examples that come with Tomcat function? TC is very picky about how it is setup. Start there and let us know.
  3. Guest

    OK, here is a pretty good walk though on setting up Tomcat for Unix. Make sure that all your permissions are setup correctly as well. If I remember correctly I had to modify some of the default permission on my Mac to get it working.

  4. macrumors 603

    What is the OS version number?

    What is the Tomcat version number?

    What instructions did you follow to install Tomcat?
    Be specific. Post a URL or give a specific book title and author.

    If I had to guess, I'd say you followed this article:
    which is from summer 2001, so is almost 10 years old.
  5. macrumors newbie

    wpotere: All of the samples in Tomcat work

    chown33: What is the OS version number? Mac OS X 10.6 client

    What is the Tomcat version number? 7.0.12

    What instructions did you follow to install Tomcat?

    You nailed it, the instructions I used came from there and seemed to work. I have also talked to someone else that suggested that this is a Java Servlet 1.0 and that Tomcat 7 is a J2EE 3.0 environment. So I wonder is it the
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    that is the problem. I had to download serverlet.jar and j2ee.jar to even compile the java file.
  6. macrumors 603


    Tomcat is not a J2EE container. Only things like Glassfish and JBoss are J2EE containers. Tomcat 7 uses the 3.0 Servlet API I believe.
  7. wpotere, Apr 27, 2011
    Last edited: Apr 27, 2011


    I'm at work right now, but I would have to look at my setup. How did you deploy? Are you using something like Netbeans to do this and latching into Tomcat using it or are you doing this manually?

    When you go to the manager page on the server admin page,http://localhost:8080 and log in, can you see the path in the list?
  8. macrumors newbie

    Yes I can see the path and the application. As a note, I tried installing tomcat 5.5.33. The install went well and I can see it when I browse port 8080. However I still get HTTP 404.

    With 5.5 i activated the invoker, and subsequently deleted the mappings in the xml part, but still no luck.

    I have also tried deploying war files, and they work. However manual deployments do not.
  9. Guest



    Can you give this web.xml a try? I "borrowed" it from a functioning example.
  10. Guest

  11. macrumors newbie

    Incorrect class name in web.xml

    Ok, so I just registered, just to answer this question... :)

    It's actually a very simple mistake.

    doesn't match

    public class Hi extends HttpServlet

    The servlet-class should be as follows:


    By the way, if you are going to be doing a lot of servlets, it would behoove you to install eclipse (http://www.eclipse.org). It's free, and includes tools to edit your web.xml, as well as starting, stopping, and debugging tomcat.

  12. macrumors newbie

    It won't compile without -cp servlet-api.jar, located in <APACHE_HOME>/lib.
    This library file needs to be in the classpath in order to compile. An easy way to find such things would have been to go to <APACHE_HOME> and run the following:

    find -name "*.jar" | xargs grep HttpServletRequest

    Assuming one of the compile errors was that it couldn't find HttpServletRequest

    Save yourself a lot of headaches, use Eclipse for this, and never have to worry about the classpath again.

  13. Guest

    Doh! Not sure how I missed that. Good catch!
  14. macrumors newbie

    It's over, I am out of time...

    Thank you all for your suggestions and input. I am convinced more now than ever that Java servlets are impossible, and how anybody can program them is a genius, more than worthy of Mensa. I tried, and tried, and tried, but I cannot get past javax.servlet.http and compiling errors. I came close to getting it to work, but all these class errors are taking up to much time.

    I hate to surrender, but I am not a programmer, and Java servlets seem to defy any sound or rational logic to me.

    To all those that tried to help me, my sincere thanks. To the person at sc.edu, I live in Columbia, SC and graduated from USC in 97. GO GAMECOCKS. If you think you can help me any more, please contact me at dmcnair@sc.rr.com.

    Thanks to all for proving MacRumros rules and cares and is a great place to get assistance, even if you are in over your head.
  15. macrumors 65816


    Java Servlets are not for the non-programmer. You might have been better off using Java ServerPages (JSPs) instead.

    But unless you need to use a Java library, have you thought have about using PHP? It's significantly more non-programmer friendly.
  16. macrumors newbie

    Hey! What's with all the doom and gloom?!

    I guess you missed my post that said how to correct that problem. The simplest solution is to use an ide, such as Eclipse. No professional Java developer would ever use the command line by itself to compile. Some might use Ant from the command line to build the entire project, but nobody would compile from the command line.

    In order to compile, you need to add the library files containing the Servlet classes.

    go to the directory containing your Hi.java file:
    cd /usr/local/apache-tomcat-7.0.12/webapps/Hi

    Here is the compile command in your case (assuming your Hi.java file is in the Hi directory)
    javac -cp ../../lib/servlet-api.jar -d WEB-INF/classes Hi.java

    What does this do?
    It includes the necessary library files to get rid of your compile errors, and puts the compiled .class file into the WEB-INF/classes directory, where it needs to be.

    Normally you would have a whole separate directory, outside of tomcat, to store your source code, but the procedure would be the same.

    Again, use Eclipse, and you won't have to worry about any of this!!!

    BTW. I realize that you specifically want to learn how to develop servlets, but really you would want to use JSP for such a simple page (actually only HTML ;) )


    PS. Here's a tutorial:

Share This Page