【问题标题】:HTTP Status 404 - The requested resource is not available Java Servlet [duplicate]HTTP 状态 404 - 请求的资源不可用 Java Servlet [重复]
【发布时间】:2016-09-18 00:37:27
【问题描述】:

我创建了动态项目 eclipse 并在 java 资源中添加了包,然后在其中添加了一个 Servlet。这是我的课程,我只是在 doGet 方法中获得了一个 hello world。 包 com.helloworldserverlet.serverlet;

import java.io.IOException;
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 {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

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

}

现在,当我尝试运行它时,我将给出错误 404,即找不到所需的资源。我是否必须在我的 web.xml 中定义 servlet。目前我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloWorld</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>

提前致谢

【问题讨论】:

  • 是的,你需要在web.xml中配置servlet
  • 不行,@WebServlet注解应该就够了。

标签: eclipse servlets http-status-code-404


【解决方案1】:

当我尝试运行它时,我会给我错误 404

可能有很多原因,最常见的有:

  • 您的应用程序上下文路径是“app”,您使用
    http://localhost:8080/HelloWorld
    运行它 正确的做法是
    http://localhost:8080/app/HelloWorld

  • 您的应用程序上下文路径是“/”,您使用
    http://localhost:8080/app/HelloWorld
    运行它 正确的做法是
    http://localhost:8080/HelloWorld

  • 您只是拼错了 servlet 路径,路径映射区分大小写。

注意:
“app”可以是您指定的任何名称,我只是用于演示。请向我们提供更多信息,例如您的完整扩展项目树和您放置 url 的浏览器位置栏。

【讨论】:

    【解决方案2】:

    您需要在web.xml 中设置更多配置。我有一个来自 Spring 的示例,在我的配置中我有:

    <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">
    
        ...
    
        <servlet>
            <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    应该不会太不同。我认为您应该将org.springframework.web.servlet.DispatcherServlet 更改为com.helloworldserverlet.serverlet.HelloWorld,或者您的Servlet 类是什么。

    【讨论】:

    • 不,OP 有 @WebServlet 注释 - 它应该可以正常工作。
    猜你喜欢
    • 2020-04-29
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多