Tomcat 5.0.28 problem - My servlet doesn't work

Discussion in 'Java' started by Thomas Hoheneder, Oct 12, 2004.

  1. Hello,

    I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
    have compiled a class TestingServlet.class with JDK 1.3.1 which is
    recommended for use with Tomcat 5.0. Let me tell you, that this is just my
    first servlet at all. For the work I use a book with the title "Java for the
    Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
    code is exactly the following, where the compiling went right:

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class TestingServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>Servlet Testing</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");
    out.println("Welcome to the Servlet Testing Center");
    out.println("</BODY>");
    out.println("</HTML>");
    }
    }

    As operating system I use Windows XP SP1 and my JAVA_HOME points to
    "C:\jdk131" which I have also configured for Tomcat. Now, the book tells me
    to put the TestingServlet.class file into a sub directory "myApps" of the
    webapps directory, which is under my Tomcat installation path. Furthermore I
    have a deployment descriptor named web.xml. So in a whole I have the
    following files:
    <TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
    <TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
    <TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class

    My deployment descriptor (web.xml) looks like as follows:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <servlet>
    <servlet-name>Testing</servlet-name>
    <servlet-class>TestingServlet</servlet-class>
    </servlet>
    </web-app>

    Then I start the Tomcat server via the Tomcat monitor. At the URL
    http://localhost:8080 I can see a standard starting page of Tomcat. Now the
    book tells me that I can use the following URL to see my TestingServlet's
    output:
    http://localhost:8080/myApp/servlet/Testing

    But this doesn't work. Instead of seeing the text "Welcome to the Servlet
    Testing Center" I get the following error message in the browser window (I
    use IE 6.0):


    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


    I have even tried the following URL's, but none of them lead to the result I
    have expected:
    http://localhost:8080/myApp/Testing
    http://localhost:8080/servlet/Testing
    http://localhost:8080/Testing
    http://localhost:8080/myApp/servlet/TestingServlet
    http://localhost:8080/myApp/TestingServlet
    http://localhost:8080/servlet/TestingServlet
    http://localhost:8080/TestingServlet
    I always get the error above.

    My question is: What went wrong? Why do I not get the text "Welcome to the
    Servlet Testing Center" of my TestingServlet displayed in the browser
    window?

    Any help to this would be very appreciated. Thanks in advance.

    Nice greetings from
    Thomas





    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    Thomas Hoheneder, Oct 12, 2004
    #1
    1. Advertising

  2. On Tue, 12 Oct 2004 18:28:30 +0200, Thomas Hoheneder
    <> wrote:

    > Hello,
    >
    > I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
    > have compiled a class TestingServlet.class with JDK 1.3.1 which is
    > recommended for use with Tomcat 5.0. Let me tell you, that this is just
    > my
    > first servlet at all. For the work I use a book with the title "Java for
    > the
    > Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
    > code is exactly the following, where the compiling went right:
    >
    > import javax.servlet.*;
    > import javax.servlet.http.*;
    > import java.io.*;
    > import java.util.*;
    >
    > public class TestingServlet extends HttpServlet {
    >
    > public void doGet(HttpServletRequest request,
    > HttpServletResponse response)
    > throws ServletException, IOException {
    >
    > PrintWriter out = response.getWriter();
    > out.println("<HTML>");
    > out.println("<HEAD>");
    > out.println("<TITLE>Servlet Testing</TITLE>");
    > out.println("</HEAD>");
    > out.println("<BODY>");
    > out.println("Welcome to the Servlet Testing Center");
    > out.println("</BODY>");
    > out.println("</HTML>");
    > }
    > }
    >
    > As operating system I use Windows XP SP1 and my JAVA_HOME points to
    > "C:\jdk131" which I have also configured for Tomcat. Now, the book tells
    > me
    > to put the TestingServlet.class file into a sub directory "myApps" of the
    > webapps directory, which is under my Tomcat installation path.
    > Furthermore I
    > have a deployment descriptor named web.xml. So in a whole I have the
    > following files:
    > <TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
    > <TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
    > <TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class


    Put all classes used in servlets into packages. Correctly name the
    package in your web.xml and save the class files accordingly.

    Old versions of servlet books and examples depended on the
    "invoker" servlet which let you get away with classes in the
    default package. BAD IDEA. See this FAQ at JavaRanch

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


    >
    > My deployment descriptor (web.xml) looks like as follows:
    >
    > <?xml version="1.0" encoding="ISO-8859-1"?>
    > <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    > http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    > version="2.4">
    > <servlet>
    > <servlet-name>Testing</servlet-name>
    > <servlet-class>TestingServlet</servlet-class>
    > </servlet>
    > </web-app>
    >
    > Then I start the Tomcat server via the Tomcat monitor. At the URL
    > http://localhost:8080 I can see a standard starting page of Tomcat. Now
    > the
    > book tells me that I can use the following URL to see my TestingServlet's
    > output:
    > http://localhost:8080/myApp/servlet/Testing
    >
    > But this doesn't work. Instead of seeing the text "Welcome to the Servlet
    > Testing Center" I get the following error message in the browser window
    > (I
    > use IE 6.0):
    >
    >
    > 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
    William Brogden, Oct 12, 2004
    #2
    1. Advertising

  3. William Brogden wrote:

    > On Tue, 12 Oct 2004 18:28:30 +0200, Thomas Hoheneder
    > <> wrote:


    [...]

    >> As operating system I use Windows XP SP1 and my JAVA_HOME points to
    >> "C:\jdk131" which I have also configured for Tomcat. Now, the book
    >> tells me
    >> to put the TestingServlet.class file into a sub directory "myApps" of the
    >> webapps directory, which is under my Tomcat installation path.
    >> Furthermore I
    >> have a deployment descriptor named web.xml. So in a whole I have the
    >> following files:
    >> <TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
    >> <TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
    >> <TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class

    >
    >
    > Put all classes used in servlets into packages. Correctly name the
    > package in your web.xml and save the class files accordingly.
    >
    > Old versions of servlet books and examples depended on the
    > "invoker" servlet which let you get away with classes in the
    > default package. BAD IDEA. See this FAQ at JavaRanch
    >
    > http://faq.javaranch.com/view?InvokerServlet


    Also, the "classes" directory should go under WEB-INF, i.e.
    <TOMCAT-HOME>/webapps/myApp/WEB-INF/classes/

    When you have done as William advised and assigned your servlet class to
    a package, its class file will need to appear in a corresponding
    subdirectory of the classes directory, e.g.
    classes/mypackage/TestingServlet.class if you assign the servlet to
    package "mypackage".


    John Bollinger
    John C. Bollinger, Oct 12, 2004
    #3
  4. Hello,

    both William and John - your advices helped. It works now. Thank you very
    much.

    Nice greetings from
    Thomas




    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    Thomas Hoheneder, Oct 12, 2004
    #4
  5. Thomas Hoheneder

    Will Hartung Guest

    "Thomas Hoheneder" <> wrote in message
    news:416c0429$1_4@127.0.0.1...
    > My question is: What went wrong? Why do I not get the text "Welcome to the
    > Servlet Testing Center" of my TestingServlet displayed in the browser
    > window?


    Couple of things, as others have mentioned.

    One, you're relying on what they call the Invoker Servlet, which makes
    Servlets in the web.xml magically appear. Tomcat disables this by default,
    as it's a bit of a security hole.

    Two, there's potentially the package issue. By not having your Servlet code
    within a Java package, you are in store for a whole lot of grief. Save for
    the most crude of example programs, and particularly within things like
    Servlet containers, always use a package.

    To get your example to work, do this:

    add:
    package mypackage;

    to the top of your Java file.

    Compile the file, and place the resulting class file in:
    WEB-INF/classes/mypackage/TestingServlet.class

    Then, fix your web.xml file, and replace:
    <servlet-class>TestingServlet</servlet-class>

    with:
    <servlet-class>mypackage.TestingServlet</servlet-class>

    Finally, again in the web.xml file, add:

    <servlet-mapping>
    <servlet-name>Testing</servlet-name>
    <url-pattern>/Testing</url-pattern>
    </servlet-mapping>

    Place that after your <servlet> tag.

    That says "When you see the URL /Testing, call the Servlet name "Testing",
    which is mapped to the 'mypackage.TestingServlet' class".

    What the older invoker Servlet did was automatically create a
    /servlet/ServletName servlet mapping for you. But, most web apps don't want
    folks calling their Servlets directly. This was the default install behavior
    in the past with Tomcat, but they removed it as many folks left this turned
    on. You could still enable this behavior, but I don't suggest it.

    After that, you should be good to go.

    Good Luck!

    Regards,

    Will Hartung
    ()
    Will Hartung, Oct 12, 2004
    #5
  6. Thomas Hoheneder

    Juha Laiho Guest

    "Thomas Hoheneder" <> said:
    >Hello,
    >
    >I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
    >have compiled a class TestingServlet.class with JDK 1.3.1 which is
    >recommended for use with Tomcat 5.0. Let me tell you, that this is just my
    >first servlet at all. For the work I use a book with the title "Java for the
    >Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
    >code is exactly the following, where the compiling went right:


    In short, please read the 'First Webapp' chapter of Tomcat 5.0
    documentation (included in Tomcat distribution, and also available at
    http://jakarta.apache.org/tomcat/tomcat-5.0-doc/index.html ).
    It even includes a small demo application.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Oct 14, 2004
    #6
    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,891
    Sudsy
    Jan 7, 2004
  2. circuit_breaker
    Replies:
    2
    Views:
    1,993
    Jack Jia
    Apr 4, 2004
  3. mojgan
    Replies:
    5
    Views:
    4,487
    William Brogden
    Jul 3, 2004
  4. Doug McCann
    Replies:
    1
    Views:
    1,001
    William Brogden
    Aug 5, 2004
  5. Replies:
    0
    Views:
    844
Loading...

Share This Page