404 Not Found Error in a simple Jetty/Maven Hello

2019-04-23 15:45发布

问题:

I have followed the instructions to create a "Standard WebApp with Jetty and Maven" precisely as described on the eclipse wiki: http://wiki.eclipse.org/Jetty/Tutorial/Jetty_and_Maven_HelloWorld#Developing_a_Standard_WebApp_with_Jetty_and_Maven

However when I run the webapp (mvn jetty:run) and go to localhost:8080/hello-world/hello I end up at a "HTTP ERROR 404 Problem accessing /hello-world/hello. Reason: Not Found". I have read through documentation, looked at the wiki page's history, poked around other forums and stackoverflow threads, but can not find the answer to this seemingly simple problem. I will post my source, but it is literally the same as the tutorial.

Any help would be appreciated. I'd really like to start playing around with this technology but its disheartening to keep slamming into the same dead end.

(Please note: the first part of the tutorial to create "JettyMavenHelloWorld" work fine. My problem is with the second part, "JettyMavenHelloWarApp". This section is titled "Developing a Standard WebApp with Jetty and Maven")

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:

The tutorial gives the incorrect url - your app context is still "/", so the urls are http://localhost:8080 and http://localhost:8080/hello for the static and dynamic content, respectively.

The maven jetty plugin documentation does claim that the default context will be named the same as the artifactId in the pom.xml, but that doesn't seem to be working here.



回答2:

I ran into the same issue and what worked for me was accessing the app like: http://localhost:8080/hello/index.jsp or http://localhost:8080/hello/index.html, whatever you're using html or js pages.



回答3:

I think adding configuration to the jetty plugin definition in your pom should change the contextpath to 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>

This is based on jetty version 9. See http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin for the configuration options.



回答4:

I had the same problem with a basic configuration.

I wanted to redirect with Spring 3 MVC an error page with this configuration (in web.xml)

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

I solve it by changing extension of error.html to error.jsp.



回答5:

I think you don't run mvn package task before mvn jetty:run that is why jetty doesn't see any sources. Just run mvn package first.



回答6:

Your servlet mapping is incorrect or insufficient.

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

You need to add a mapping for the URL pattern "/hello"

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