简单的 Jetty/Maven Hello World webapp 中的 404 Not Found 错误

Posted

技术标签:

【中文标题】简单的 Jetty/Maven Hello World webapp 中的 404 Not Found 错误【英文标题】:404 Not Found Error in a simple Jetty/Maven Hello World webapp 【发布时间】:2012-09-09 04:28:41 【问题描述】:

我已按照 Eclipse wiki 中的说明创建“带有 Jetty 和 Maven 的标准 WebApp”准确:http://wiki.eclipse.org/Jetty/Tutorial/Jetty_and_Maven_HelloWorld#Developing_a_Standard_WebApp_with_Jetty_and_Maven

但是,当我运行 webapp (mvn jetty:run) 并转到 localhost:8080/hello-world/hello 时,我最终会遇到“访问 /hello-world/hello 时出现 HTTP ERROR 404 问题。原因:未找到” .我通读了文档,查看了 wiki 页面的历史,浏览了其他论坛和 *** 线程,但找不到这个看似简单问题的答案。我将发布我的源代码,但它实际上与教程相同。

任何帮助将不胜感激。我真的很想开始使用这项技术,但不断陷入同样的​​死胡同令人沮丧。

(请注意:教程的第一部分创建“JettyMavenHelloWorld”工作正常。我的问题是第二部分“JettyMavenHelloWarApp”。本节标题为“使用 Jetty 和 Maven 开发标准 WebApp”)

JettyMavenHelloWarApp/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Jetty HelloWorld</name>

  <properties>
    <jettyVersion>7.2.0.v20101020</jettyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>$jettyVersion</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <!-- This plugin is needed for the servlet example -->
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>$jettyVersion</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution><goals><goal>java</goal></goals></execution>
        </executions>
        <configuration>
          <mainClass>org.example.HelloWorld</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

JettyMavenHelloWarApp/src/main/java/org/example/HelloServlet.java

package org.example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello Servlet</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    

JettyMavenHelloWarApp/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>Hello</servlet-name>
    <servlet-class>org.example.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>
</web-app>

JettyMavenHelloWarApp/src/main/webapp/index.html

<h1>Hello World Webapp</h1>
<a href="/hello">Hello Servlet</a>

【问题讨论】:

【参考方案1】:

您的 servlet 映射不正确或不足。

<url-pattern>/hello/*</url-pattern> // does not respond to "/hello"

您需要为 URL 模式“/hello”添加映射

<url-pattern>/hello</url-pattern>

【讨论】:

【参考方案2】:

我认为您不会在 mvn jetty:run 之前运行 mvn package 任务,这就是码头看不到任何来源的原因。只需先运行mvn package

【讨论】:

【参考方案3】:

我在基本配置方面遇到了同样的问题。

我想用 Spring 3 MVC 重定向一个带有此配置的错误页面(在 web.xml 中)

<error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/error.html</location>
</error-page>

我通过将 error.html 的扩展名更改为 error.jsp 来解决它。

【讨论】:

【参考方案4】:

我遇到了同样的问题,对我有用的是访问应用程序,例如:http://localhost:8080/hello/index.jsphttp://localhost:8080/hello/index.html,无论您使用的是 html 还是 js 页面。

【讨论】:

【参考方案5】:

我认为向 pom 中的 jetty 插件定义添加配置应该会将 contextpath 更改为 hello-world:

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>$jettyVersion</version>
            <configuration>
                <webApp>
                    <contextPath>/hello-world</contextPath>
                </webApp>
            </configuration>
        </plugin>

这是基于 jetty 版本 9。有关配置选项,请参阅 http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin。

【讨论】:

【参考方案6】:

教程给出了不正确的 url - 你的应用上下文仍然是“/”, 所以静态和动态内容的 url 分别是 http://localhost:8080http://localhost:8080/hello

maven jetty 插件文档确实声称默认上下文将被命名为与 pom.xml 中的 artifactId 相同,但这似乎在这里不起作用。

【讨论】:

以上是关于简单的 Jetty/Maven Hello World webapp 中的 404 Not Found 错误的主要内容,如果未能解决你的问题,请参考以下文章

Maven: clean jetty:run报如下错,请问是啥问题?

如何用jetty maven插件运行web项目

今日出错解决cannot resolve plugin org.mortbay.jetty:maven-jetty-plugin阿里云仓库对应地址没有jetty插件

使用 jetty maven 插件优雅关闭 Java 服务

Jetty 嵌入式开发(实例)

xml Jetty Maven插件配置;包括端口号