• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

About executing servlet class

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you create a mapping for your servlet in web.xml?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry!
when i run it i write:
http://localhost:8080/Manning/hello
I think i am very sleepy!
 
Ailsa Cape
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic