提问者:小点点

404在一个简单的码头/Maven Hello World webapp中未找到错误


我已经按照指示创建了一个“带有Jetty和Maven的标准WebApp”,正如eclipse wiki上所描述的那样: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时,我最终得到了一个“HTTP错误404访问问题 /hello-world/hello.原因:未找到”。我通读了留档,查看了wiki页面的历史记录,在其他论坛和stackoverflow线程中寻找,但找不到这个看似简单的问题的答案。我会发布我的源代码,但它实际上与教程相同。

任何帮助都将不胜感激。我真的很想开始玩这项技术,但是继续陷入同样的死胡同令人沮丧。

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

Jetty Maven Hello War App/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>

Jetty Maven Hello War App/s rc/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());
    }
}

Jetty Maven Hello War App/s rc/main/web app/WEB-IN F/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>

Jetty Maven Hello War App/s rc/main/web app/index. html

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

共3个答案

匿名用户

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

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

匿名用户

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

匿名用户

我认为将配置添加到pom中的jetty插件定义应该将上下文路径更改为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>

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