尝试调用servlet时遇到ERROR 404

时间:2011-11-07 06:12:35

标签: java apache jsp tomcat servlets

我正在尝试使用apache tomcat 7.0 tomcat7来访问java servlet。

我创建了一个文件夹,用于保存webapps中ROOT文件夹下的所有webapp文件。 文件结构是这样的。

  • Tomcat 7.0 / webapps / myWebApp
    • HelloHome.html
    • WEB-INF
      • 的web.xml
        • COM
          • 训练
            • HelloServlet.java
            • HelloServlet.class

调用servlet使用了这个web.xml代码:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="3.0"
  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_3_0.xsd">

   <!-- To save as "hello\WEB-INF\web.xml" -->

   <servlet>
      <servlet-name>HelloWorld</servlet-name>
      <servlet-class>com.training.HelloServlet</servlet-class>
   </servlet>

   <!-- Note: All <servlet> elements MUST be grouped together and
         placed IN FRONT of the <servlet-mapping> elements -->

   <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/sayhello</url-pattern>
   </servlet-mapping>

</web-app>

实际的java代码(编译为在classes文件夹下生成一个类文件):

//To save as "<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.java"
package com.training;

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

public class HelloServlet extends HttpServlet {
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {

      // Set the response MIME type of the response message
      response.setContentType("text/html");
      // Allocate a output writer to write the response message into the network socket
      PrintWriter out = response.getWriter();

      // Write the response message, in an HTML page
      try {
         out.println("<html>");
         out.println("<head><title>Hello, World</title></head>");
         out.println("<body>");
         out.println("<h1>Hello, world!</h1>");  // says Hello
         // Echo client's request information
         out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
         out.println("<p>Protocol: " + request.getProtocol() + "</p>");
         out.println("<p>PathInfo: " + request.getPathInfo() + "</p>");
         out.println("<p>Remote Address: " + request.getRemoteAddr() + "</p>");
         // Generate a random number upon each request
         out.println("<p>A Random Number: <strong>" + Math.random() + "</strong></p>");
         out.println("</body></html>");
      } finally {
         out.close();  // Always close the output writer
      }
   }
}
然后点击: http:// localhost:8080 / myWebApp / sayhello
并得到404错误。关于我做错了什么想法?

5 个答案:

答案 0 :(得分:1)

myWebApp应该住在ROOT旁边,而不是它内部。

Tomcat 7 Deployment

答案 1 :(得分:1)

将myWebApp.war文件放在webapps文件夹而不是ROOT下,重新启动Tomcat,然后尝试使用相同的URL。

答案 2 :(得分:0)

尝试将servlet放入包中,并相应地在web.xml中注册。 我记得几年前我用旧版tomcat遇到过这个问题。

答案 3 :(得分:0)

ROOT将拥有tomcat演示应用程序, 将myWebApp文件夹放在$ tomcat_home / webapps(不在ROOT内)

重启tomcat&amp;尝试相同的网址

答案 4 :(得分:0)

编辑您的web.xml文件并将其添加到以下标记:

&LT;欢迎-文件列表&gt;

&LT;欢迎-文件&gt; /&的SayHello LT; /欢迎-文件&gt;

&LT; /欢迎-文件列表&gt;

&LT; / web应用程序&GT;