【问题标题】:Tomcat 6.0 - Error 404 requested resource not avaliableTomcat 6.0 - 错误 404 请求的资源不可用
【发布时间】:2012-09-12 16:22:38
【问题描述】:

我正在使用 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 错误。

我能做什么?提前致谢

【问题讨论】:

  • 检查拼写错误goGe应该是doGet

标签: java servlets tomcat6


【解决方案1】:

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

/var/lib/tomcat6/webapps/myapp

Documentation

【讨论】:

    【解决方案2】:

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

    /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>"); 
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多