Help with Java Servlets - Using Tomcat 6

Discussion in 'Programming' started by Kameel, Aug 1, 2009.

  1. #1
    Hi

    I'd like to know if anyone could help me with executing a servlet on Tomcat.
    I'm pretty sure that my installation is correct, because the server is displaying my HTML's and JSP's.
    It's just a problem with servlets.
    This is the error I get:
    HTTP Status 404 - /servlet/RequestType

    type Status report

    message /servlet/RequestType

    description The requested resource (/servlet/RequestType) is not available.
    Apache Tomcat/6.0.18


    My servlet class is stored in /install-dir/webapps/ROOT/WEB-INF/classes
    The URL i'm using for access is http://localhost:8080/servlet/className

    The example servlets from tomcat deploy perfectly, but when i move them to my above directory then they don't work.

    NB, i'm using Linux (Fedora 10)
     
    Kameel, Aug 1, 2009 IP
  2. hotpop

    hotpop Peon

    Messages:
    2,059
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi, I think you should configure web.xml to map the servlet.
    <servlet>
    <servlet-name>servlet-name</servlet-name>
    <servlet-class>com.myapp.servlets.MyServlet</servlet-class>
    </servlet>
    Hope it helps.
     
    hotpop, Aug 1, 2009 IP
  3. Kameel

    Kameel Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi

    I've tried that, but it doesn't seem to work:(
     
    Kameel, Aug 1, 2009 IP
  4. hotpop

    hotpop Peon

    Messages:
    2,059
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Would you like to attach your web.xml and servlet files?
    So I can know how to fix it
     
    hotpop, Aug 1, 2009 IP
  5. Kameel

    Kameel Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    XML File
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements. See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    -->

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>Welcome to Tomcat</display-name>
    <description>
    Welcome to Tomcat
    </description>
    <servlet>
    <servlet-name>RequestType</servlet-name>
    <servlet-class>com.myapp.servlets.RequestType</servlet-class>
    </servlet>

    </web-app>


    Servlet Source Code
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class RequestType extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException {
    rsp.setContentType("text/html");
    PrintWriter out = rsp.getWriter();

    out.println("<html>");
    out.println("<head><title> Request Type: GET </title></head>");
    out.println("<body>");
    out.println("<p>This page is the result of a GET request.</p>");
    out.println("</body></html>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse rsp)
    throws ServletException, IOException {
    rsp.setContentType("text/html");
    PrintWriter out = rsp.getWriter();

    out.println("<html>");
    out.println("<head><title> Request Type: POST </title></head>");
    out.println("<body>");
    out.println("<p>This page is the result of a POST request.</p>");
    out.println("</body></html>");
    }

    }

    I'm really sorry if i pasted the code incorrectly.
    I haven't pasted code here before.

    I also tried changing <servlet-class>com.myapp.servlets.RequestType</servlet-class> to <servlet-class>com.myapp.servlets.MyServlet</servlet-class>
    and to <servlet-class>RequestType</servlet-class>.

    I don't really understand XML.
    I was also told to edit the web.xml in the conf folder. I uncommented
    the servlet-mapping element for the invoker servlet, it didn't work either.
     
    Kameel, Aug 1, 2009 IP
  6. Kameel

    Kameel Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Also my folder structure is that:
    ROOT contains the WEB-INF folder.
    The WEB-INF folder contains the web.xml file and the classes folder (where the servlet .class file is located)

    Thanks
     
    Kameel, Aug 1, 2009 IP