problem running a basic servlet with tomcat

Discussion in 'Java' started by Ryan Gaffuri, Sep 27, 2004.

  1. Ryan Gaffuri

    Ryan Gaffuri Guest

    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.


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

    Apache Tomcat/5.0.28
    Ryan Gaffuri, Sep 27, 2004
    #1
    1. Advertising

  2. Ryan Gaffuri

    ryan Guest

    The wierd thing is that if I go to:

    http://localhost:8080/examples/servlet/HelloWorldExample

    and run some examples it works fine.

    I copied my 'testingServlet'

    to the examples/WEB-INF/classes directory.. and it works.

    any idea why? My directory structure has to be right. I compared it to the
    examples directory.
    "Ryan Gaffuri" <> wrote in message
    news:...
    > 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.
    >
    >
    > --------------------------------------------------------------------------

    ------
    >
    > Apache Tomcat/5.0.28
    ryan, Sep 27, 2004
    #2
    1. Advertising

  3. Ryan Gaffuri

    Sudsy Guest

    ryan wrote:
    > The wierd thing is that if I go to:
    >
    > http://localhost:8080/examples/servlet/HelloWorldExample
    >
    > and run some examples it works fine.
    >
    > I copied my 'testingServlet'
    >
    > to the examples/WEB-INF/classes directory.. and it works.


    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...
    Sudsy, Sep 27, 2004
    #3
  4. On 26 Sep 2004 17:11:00 -0700, Ryan Gaffuri <> wrote:

    > 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
    William Brogden, Sep 27, 2004
    #4
  5. Ryan Gaffuri

    Ryan Gaffuri Guest

    Sudsy <> wrote in message news:<>...
    > ryan wrote:
    > > The wierd thing is that if I go to:
    > >
    > > http://localhost:8080/examples/servlet/HelloWorldExample
    > >
    > > and run some examples it works fine.
    > >
    > > I copied my 'testingServlet'
    > >
    > > to the examples/WEB-INF/classes directory.. and it works.

    >
    > 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.
    Ryan Gaffuri, Sep 27, 2004
    #5
  6. Ryan Gaffuri

    Mark Marcus Guest

    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:

    On 26 Sep 2004 17:11:00 -0700, (Ryan Gaffuri) wrote:

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

    >
    >6. I type the following URL in:
    >
    >http://localhost:8080/myApp/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.
    >
    >
    >--------------------------------------------------------------------------------
    >
    >Apache Tomcat/5.0.28



    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
    Mark Marcus, Sep 27, 2004
    #6
  7. Ryan Gaffuri

    Ryan Gaffuri Guest

    Sudsy <> wrote in message news:<>...
    > ryan wrote:
    > > The wierd thing is that if I go to:
    > >
    > > http://localhost:8080/examples/servlet/HelloWorldExample
    > >
    > > and run some examples it works fine.
    > >
    > > I copied my 'testingServlet'
    > >
    > > to the examples/WEB-INF/classes directory.. and it works.

    >
    > 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).
    Ryan Gaffuri, Sep 27, 2004
    #7
  8. On 27 Sep 2004 08:56:58 -0700, Ryan Gaffuri <> wrote:

    > Sudsy <> wrote in message
    > news:<>...
    >> ryan wrote:
    >> > The wierd thing is that if I go to:
    >> >
    >> > http://localhost:8080/examples/servlet/HelloWorldExample
    >> >
    >> > and run some examples it works fine.
    >> >
    >> > I copied my 'testingServlet'
    >> >
    >> > to the examples/WEB-INF/classes directory.. and it works.

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


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

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

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
    William Brogden, Sep 27, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Clarke
    Replies:
    1
    Views:
    1,881
    Sudsy
    Jan 7, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,984
    Jack Jia
    Apr 4, 2004
  3. Doug McCann
    Replies:
    1
    Views:
    993
    William Brogden
    Aug 5, 2004
  4. Replies:
    0
    Views:
    836
  5. Replies:
    4
    Views:
    770
Loading...

Share This Page