problem running a basic servlet with tomcat

R

Ryan Gaffuri

My book is about tomcat 4.0. I am using tomcat 5.0. I am trying to run
a servlet on my computer.
I am getting 404 errors.

I have done the following:

1. created: testingServlet.java

2. compiled it to testingSerlvet.class

3. I have the following directory structure setup:
c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\classes

4. I navigate to the Tomcat_5\bin directory and type 'startup' to
start tomcat.

5. I have the following web.xml file in the following directory:

c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\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>testing</servlet-name>
<servlet-class>testingServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>testing</servlet-name>
<url-pattern>/servlet/Testing</url-pattern>
</servlet-mapping></web-app>


6. I type the following URL in:

http://localhost:8080/myApp/servlet/testing

7. Here is the code for my class:


import java.io.*;import javax.servlet.*;import
javax.servlet.http.*;public class testingServlet 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>Hello
World!</title>"); out.println("</head>");
out.println("<body>"); out.println("<h1>Hello World!</h1>");
out.println("</body>"); out.println("</html>"); }}


8. I get the following errors:

HTTP Status 404 - /myApp/servlet/testing

--------------------------------------------------------------------------------

type Status report

message /myApp/servlet/testing

description The requested resource (/myApp/servlet/testing) is not
available.
 
W

William Brogden

My book is about tomcat 4.0. I am using tomcat 5.0. I am trying to run
a servlet on my computer.
I am getting 404 errors.

I have done the following:

1. created: testingServlet.java

2. compiled it to testingSerlvet.class

3. I have the following directory structure setup:
c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\classes

4. I navigate to the Tomcat_5\bin directory and type 'startup' to
start tomcat.

5. I have the following web.xml file in the following directory:

c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\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>testing</servlet-name>
<servlet-class>testingServlet</servlet-class> </servlet>

Bad idea - testingServlet should be in a package. All classes
used in servlets should be in packages.

<servlet-mapping> <servlet-name>testing</servlet-name>
<url-pattern>/servlet/Testing</url-pattern>
</servlet-mapping></web-app>


6. I type the following URL in:

http://localhost:8080/myApp/servlet/testing

Bad idea - that url depends on the "invoker" servlet - invoker
used to be turned on by default but starting in mid Tomcat 4 it
is turned on only in the /examples/ directory where it continues
to confuse people. See the remarks about "invoker" in the default
web.xml file of your Tomcat installation.

This faq is also useful

http://faq.javaranch.com/view?InvokerServlet
 
R

Ryan Gaffuri

Sudsy said:
So where have you defined the "myApp" context? Take a good
look at the configuration files for the "example" context.
They should provide the framework for the XML file you're
apparently missing...

i am using the book 'Java for the Web with Servlets' by Budi
Kurniawan. Its written for tomcat 4.0. Nothing in here whatsoever
about contexts.

where can i get info on this? I am trying to use it with either tomcat
4.1.3 or 5.0. I couldnt find tomcat 4.0 on the apache site. I guess
its too old.
 
M

Mark Marcus

I'd check first wtih catalina.out (should be in the tomcat logs
directory) to see if anything didn't start right. Also look into
this:

My book is about tomcat 4.0. I am using tomcat 5.0. I am trying to run
a servlet on my computer.
I am getting 404 errors.

I have done the following:

1. created: testingServlet.java

2. compiled it to testingSerlvet.class

3. I have the following directory structure setup:
c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\classes

4. I navigate to the Tomcat_5\bin directory and type 'startup' to
start tomcat.

5. I have the following web.xml file in the following directory:

c:\Program Files\Apache\Tomcat_5\webapps\myApp\WEB-INF\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>testing</servlet-name>
<servlet-class>testingServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>testing</servlet-name>
<url-pattern>/servlet/Testing</url-pattern>
</servlet-mapping></web-app>

The URL pattern you matched to is /servlet/Testing ...

The URL you typed in is /servlet/testing ...

7. Here is the code for my class:


import java.io.*;import javax.servlet.*;import
javax.servlet.http.*;public class testingServlet 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>Hello
World!</title>"); out.println("</head>");
out.println("<body>"); out.println("<h1>Hello World!</h1>");
out.println("</body>"); out.println("</html>"); }}


8. I get the following errors:

HTTP Status 404 - /myApp/servlet/testing

--------------------------------------------------------------------------------

type Status report

message /myApp/servlet/testing

description The requested resource (/myApp/servlet/testing) is not
available.


Once upon a time, long time ago, before the age of the dinosaurs, I
was playing with some really old implementations of a mod_serv. And I
vaguely recall that the classes I made had to be packaged or it didn't
work. ... Might want to try that too (even though that shouldn't be
the case nowadays).



Mark Marcus
Protect Your Email Address and Make Money too!
http://www.xhome.org My Sales Code is 22819
 
R

Ryan Gaffuri

Sudsy said:
So where have you defined the "myApp" context? Take a good
look at the configuration files for the "example" context.
They should provide the framework for the XML file you're
apparently missing...

i did some further tests. I do not know tomcat well and my book is
wrong, so its try and error for me.

I know that i can put my servlets in the 'webapps/examples' directory
and get them to run, but if i move them to a new 'webapps/myApp'
directory i get 404 errors.

1. I thought it might be the web.xml file. so i copied the web.xml
file from examples to my new directory structure. Still failed.

2. I then decided to remove the web.xml file from the /examples
directory to see if i can run servlets without this file. They ran.

I stopped and restarted tomcat before each change. So I have come to
the conclusion that my web.xml file in the directories is not the
problem.

Part 2:

There is a web.xml file and a server.xml file in the /conf directory.

I noticed that the server.xml file had a context entry for the
/examples directory. I made the following change:


<!-- Tomcat Examples Context -->
<Context path="/myApp" docBase="myApp" debug="0"

I can still run servlets under the /examples directory, but not under
the /myApp directory.


Since I do not know tomcat well and my book is wrong for versions
newer than 4.0, can someone please help me out here? I have narrowed
it down, i dont think its the web.xml or server.xml files(please see
above reasons).
 
W

William Brogden

i did some further tests. I do not know tomcat well and my book is
wrong, so its try and error for me.

I know that i can put my servlets in the 'webapps/examples' directory
and get them to run, but if i move them to a new 'webapps/myApp'
directory i get 404 errors.

1. I thought it might be the web.xml file. so i copied the web.xml
file from examples to my new directory structure. Still failed.

2. I then decided to remove the web.xml file from the /examples
directory to see if i can run servlets without this file. They ran.

I stopped and restarted tomcat before each change. So I have come to
the conclusion that my web.xml file in the directories is not the
problem.

Part 2:

There is a web.xml file and a server.xml file in the /conf directory.

I noticed that the server.xml file had a context entry for the
/examples directory. I made the following change:


<!-- Tomcat Examples Context -->
<Context path="/myApp" docBase="myApp" debug="0"

I can still run servlets under the /examples directory, but not under
the /myApp directory.


Since I do not know tomcat well and my book is wrong for versions
newer than 4.0, can someone please help me out here? I have narrowed
it down, i dont think its the web.xml or server.xml files(please see
above reasons).

Probably you are using URLs that include "/servlet/" - which depend
on the "invoker" servlet. See this JavaRanch FAQ:

http://faq.javaranch.com/view?InvokerServlet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top