在一个简单的Jetty / Maven Hello World webapp中找不到404错误

时间:2012-09-16 21:58:13

标签: maven maven-2 jetty embedded-jetty

我已按照说明创建了“标准WebApp with Jetty and Maven”正好,如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 ERROR 404问题访问/ hello-world / hello。原因:找不到” 。我已经阅读了文档,查看了wiki页面的历史,探讨了其他论坛和stackoverflow线程,但找不到这个看似简单问题的答案。我将发布我的源代码,但它与教程完全相同。

任何帮助将不胜感激。我真的很想开始玩这种技术,但它仍然沮丧地继续抨击同样的死胡同。

(请注意:创建“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 /主/ JAVA /组织/示例/ 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 /主/ web应用/ 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 /主/ web应用/ index.html中

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

6 个答案:

答案 0 :(得分:12)

教程提供了错误的网址 - 您的应用内容仍为“/”, 因此,对于静态和动态内容,网址分别为http://localhost:8080http://localhost:8080/hello

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

答案 1 :(得分:3)

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

答案 2 :(得分:0)

我认为将配置添加到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

答案 3 :(得分:0)

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

我想使用此配置(在web.xml中)将Spring 3 MVC重定向到错误页面

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

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

您需要为网址格式“ / hello”添加映射

<url-pattern>/hello</url-pattern>
相关问题