servlet程序HTTP状态404时出错 - / HelloWorldServlet /

时间:2013-11-13 07:30:29

标签: java servlets tomcat7 eclipse-juno

我的Servlets计划

package com.srccodes.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Helloworld
 */
@WebServlet("/Helloworld")
public class Helloworld extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Helloworld() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter printWriter  = response.getWriter();
        printWriter.println("<h1>Hello World!</h1>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

更新:

wec.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloWorldServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在tomcat服务器上运行上面的程序时出现错误 错误 - &gt; http://i.stack.imgur.com/655Ew.png。 如何解决这个问题,请尽量建议我,好的答案将适用

3 个答案:

答案 0 :(得分:1)

他们在您上面提供的代码中没有错。 您的servlet名称是“Helloworld”,您尝试的URL是

http://localhost:8080/HelloworldServlet

如果要运行相同的servlet,请尝试

http://localhost:8080/HelloworldServlet/Helloworld

还要重新启动服务器并检查URL。

答案 1 :(得分:0)

从网址中删除“Servlet”,例如http://localhost:8080/Helloworld

答案 2 :(得分:0)

此类是WAR应用程序的一部分吗?尝试在端口和HelloWorld之间放置WAR名称。