It's not a secret anymore!
The moose likes Tomcat and the fly likes About executing servlet class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "About executing servlet class" Watch "About executing servlet class" New topic

About executing servlet class

Ailsa Cape
Ranch Hand

Joined: Aug 10, 2005
Posts: 92
Hello, everyone

I install the Apache Tomcat 5.0.28 to run my servlet class-TestServlet.

I set the parameters in the Windows 2000 system as following,
"classpath" , which is ".;C:\Java\jdk1.5.0_02\lib\tools.jar;C:\Java\jdk1.5.0_02\lib\dt.jar;C:\Tomcat\common\lib\servlet-api.jar",
"path", which is "C:\Java\jdk1.5.0_02\bin",
and "JAVA_HOME", which is "C:\Java\jdk1.5.0_02".

I place the TestServlet.class file in the "C:\Tomcat\webapps\test\WEB-INF\classes" directory. After I started the Apache Tomcat, I use the "http://localhost:8080/test/servlet/TestServlet" URL in the IE explorer, but I got an information about "HTTP Status 404 - /test/servlet/TestServlet".

While I use a servlet example which is provided by Tomcat, It works well. For example, when I use this URL, "http://localhost:8080/servlets-examples/servlet/RequestParamExample" , it displays quite well without error information. The RequestParamExample.class locates in the "C:\Tomcat\webapps\servlets-examples\WEB-INF\classes" directory, I think its deployment is the same as my TestServlet class, but why I get this "404 error"?

Thanks in advance!
Regards, Ailsa Cape
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Did you create a mapping for your servlet in web.xml?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
Some points to add to what Ben said
1. Tomcat ignores the environment variable "classpath"
2. Your servlet class should be in a package and that package name used in the web.xml mapping.
3. Your use of "/servlet/" in the URL suggests you may be depending on the infamous "invoker" servlet. See this FAQ here at the ranch for why this is a bad idea.
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
I had the same problem, but I followed what Ben, William and the link that William provided said. I can see "Hello World!" now.
I took the advice in the FAQ and didn't uncomment the code, instead i added the mapping to the web.xml.
Here it is the
package chapter01;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
PrintWriter pw = response.getWriter();
pw.println("<h3>Hello World!</h3>");

and here it is the web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"





Now to the directories:
Under <Tomcat installation directory>\webapps I added a directory called Manning (it is not important what you call it). The tree (and the files) under this directory is:
Now when i run it, i write:

Good luck!
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
when i run it i write:
I think i am very sleepy!
Ailsa Cape
Ranch Hand

Joined: Aug 10, 2005
Posts: 92
Hi, Nawar,Ben and William

Thank you all!
After I wrote the same web.xml file as what Nawar did, the servlet class works quite well. Now I understand the importance of the web.xml file in Tomcat 5.

Regards, Ailsa Cape
I agree. Here's the link:
subject: About executing servlet class
It's not a secret anymore!