java - servlet 程序 HTTP Status 404 -/HelloWorldServlet/时出现错误

标签 java servlets tomcat7 eclipse-juno

我的 Servlet 程序

包 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 服务器上运行上述程序时出现错误 错误 --> /image/655Ew.png 。 如何解决这个问题,请尝试建议我,好的答案将不胜感激

最佳答案

您上面提供的代码没有任何问题。 您的 servlet 名称是“Helloworld”,您尝试的 URL 是

http://localhost:8080/HelloworldServlet

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

http://localhost:8080/HelloworldServlet/Helloworld

同时重新启动服务器并检查 URL。

关于java - servlet 程序 HTTP Status 404 -/HelloWorldServlet/时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947889/

相关文章:

java - 在Java中将字符串中的更多空白空间折叠成单个空白空间?

java - 非顺序地构建一个字符串?

java - 具有半透明 JTextArea 的工件

java - 参数和属性之间的区别

Maven 配置文件未使用 maven-tomcat-plugin 部署来纠正 tomcat

android - 服务器选项卡在 eclipse v3.7.2 中不可见

java - 使用 Spring 的 JavaMailSender 发送的 Html 邮件没有主题和纯文本

java - Eclipse 中的 Apache Velocity 错误 [Java、Eclipse Luna、Velocity 2.0、VelocityView、Tomcat 7]

java - 从 servlet 创建自定义 json

docker - 无法连接到 Windows 上的 docker tomcat 容器 - 我做错了什么?