0

我正在使用 Tomcat Servlet Engine(6.0 版)开发一个 Servlet

我是这个东西的新手,所以我正在网上阅读一些东西。现在我在下面创建我的 servlet 文件夹:

/var/lib/tomcat6/webapps/ROOT/myapp

在此我创建 WEB-INF 文件夹和类之一。所以我有这个层次结构:

/var/lib/tomcat6/webapps/ROOT/myapp/WEB-INF/classes

我正在尝试执行这个简单的 servlet:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{

    public void doGe(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Hello Servlet!</h1>");
        out.println("</body>");
        out.println("</html>"); 
    }
}

编译后我把.class文件,当然在classes目录下,然后我在WEB-INF目录下创建了web.xml文件,这里不是内容:

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

    <description>
      Servlet and JSP Examples.
    </description>
    <display-name>Servlet and JSP Examples</display-name>

    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>TestServlet</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

然后我简单地去:

   http://localhost:8080/myapp/test

但我收到 404 错误。

我能做些什么?提前致谢

4

2 回答 2

2

将文件夹从ROOT移动到webapp,文件夹结构应该是

/var/lib/tomcat6/webapps/myapp

文档

于 2012-09-20T10:07:08.387 回答
1

您无需移动 Root 文件夹下的 Web 应用程序项目。保持为:

/var/lib/tomcat6/webapps/myapp

另外,对方法签名进行更正doGet(HttpServletRequest request, HttpServletResponse response) {}

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Hello Servlet!</h1>");
        out.println("</body>");
        out.println("</html>"); 
    }

就是这样,它现在应该可以工作了!

于 2012-09-20T10:09:44.650 回答