基本的Tomcat Servlet错误

时间:2010-04-05 01:18:06

标签: tomcat servlets

package mypackage;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class Hello extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}

web.xml文件如下:

<?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">

    <display-name>Hello, World Application</display-name>
    <description>
    This is a simple web application with a source code organization
    based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>mypackage.Hello</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

我正在做ant all,ant reload以及/etc/init.d/tomcat restart

但我在浏览器上收到此错误:

HTTP状态404 - / hello /

输入状态报告

message / hello /

description请求的资源(/ hello /)不可用。 Apache Tomcat / 6.0.26

可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

我认为目录结构有问题。通常你在目录结构下面做

webapps
  MyWebApp1
  MyWebApp2

如果您的应用程序已复制到MyWebApp1文件夹,请致电localhost/MyWebApp1/hello

所以目录将是

webapps
  +MyWebApp1
    test.jsp
    WEB-INF
      web.xml
      +classes
        +mypackage
          hello.class
  +MyWebApp2