Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ddmcnair

macrumors member
Original poster
Apr 25, 2011
63
11
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:

Hi.java
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
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Hola</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1> Hi </h1>");
out.println("</body>");
out.println("</html>");
}
}

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

<servlet>
<servlet-name>Hi</servlet-name>
<description>TestingExample</description>
<servlet-class>HiExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Hi</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hi</servlet-name>
<url-pattern>/Hi</url-pattern>
</servlet-mapping>

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.
 
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.
 
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?
http://developer.apple.com/internet/java/tomcat1.html

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.
 
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?
 
Last edited:
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.
 
Here is the web.xml file:
<display-name>My Web Application</display-name>

<servlet>
<servlet-name>Hi</servlet-name>
<description>TestingExample</description>
<servlet-class>HiExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Hi</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Hi</servlet-name>
<url-pattern>/Hi</url-pattern>
</servlet-mapping>

<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HiExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

Can you give this web.xml a try? I "borrowed" it from a functioning example.
 
Incorrect class name in web.xml

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

It's actually a very simple mistake.
<servlet-class>HiExample</servlet-class>

doesn't match

public class Hi extends HttpServlet

The servlet-class should be as follows:

<servlet-class>Hi</servlet-class>

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.

Gary
 
wpotere: All of the samples in Tomcat work
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.

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.

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

It's actually a very simple mistake.
<servlet-class>HiExample</servlet-class>

doesn't match

public class Hi extends HttpServlet

The servlet-class should be as follows:

<servlet-class>Hi</servlet-class>



Gary

Doh! Not sure how I missed that. Good catch!
 
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.
 
I hate to surrender, but I am not a programmer, and Java servlets seem to defy any sound or rational logic to me.

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.
 
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.

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 ;) )

Gary

PS. Here's a tutorial:
http://www.vogella.de/articles/EclipseWTP/article.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.