aspose file tools*
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
Author

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
Sheriff

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
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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.
Bill
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 HelloWorldServlet.java:
package chapter01;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Hello World!</h3>");
pw.println("</body>");
pw.println("</html>");
}
}

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"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>chapter01.HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>

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:
Manning\WEB-INF\web.xml
Manning\WEB-INF\classes\chapter01\HelloWorldServlet.class
Now when i run it, i write:
http://localhost:8080/chapter01/hello

Good luck!
Nawar Gailani
Ranch Hand

Joined: Feb 08, 2005
Posts: 46
sorry!
when i run it i write:
http://localhost:8080/Manning/hello
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: http://aspose.com/file-tools
 
subject: About executing servlet class