根据说明使用 Maven 构建的简单 Jetty 应用 returns 404

Simple Jetty app built with Maven according to instructions returns 404

我正在尝试按照以下网址的说明进行操作:

https://www.eclipse.org/jetty/documentation/9.4.x/maven-and-jetty.html

我创建了三个文件:

/Users/dlynch/eclipse-workspace2/swbpp/src/main/java/org/firezeal/SwbppServlet.java

package org.firezeal;

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 SwbppServlet extends HttpServlet
{
    @Override
    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());
    }
}

/Users/dlynch/eclipse-workspace2/swbpp/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
   metadata-complete="false"
   version="3.1">

  <servlet>
    <servlet-name>swbpp</servlet-name>
    <servlet-class>org.firezeal.SwbppServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>swbpp</servlet-name>
    <url-pattern>/swbpp/*</url-pattern>
  </servlet-mapping>

</web-app>

/Users/dlynch/eclipse-workspace2/swbpp/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.firezeal</groupId>
  <artifactId>SwbppServlet</artifactId>
  <version>1</version>
  <packaging>war</packaging>
  <name>Jetty HelloWorld WebApp</name>

  <properties>
      <maven.compiler.source>1.6</maven.compiler.source>
      <maven.compiler.target>1.6</maven.compiler.target>
      <jettyVersion>9.4.9.v20180320</jettyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jettyVersion}</version>
      </plugin>
    </plugins>
  </build>

</project>

Maven 可以很好地编译项目。然后我运行码头:

MacBook-Pro-504% java -jar "${JETTY_HOME}"/start.jar
2018-04-06 09:46:09.670:INFO::main: Logging initialized @596ms to org.eclipse.jetty.util.log.StdErrLog
2018-04-06 09:46:09.916:INFO:oejs.Server:main: jetty-9.4.9.v20180320; built: 2018-03-20T05:21:10-07:00; git: 1f8159b1e4a42d3f79997021ea1609f2fbac6de5; jvm 9.0.4+11
2018-04-06 09:46:09.928:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:///Users/dlynch/Services/Jetty-Base/swbpp/webapps/] at interval 1
2018-04-06 09:46:10.015:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /SwbppServlet-1, did not find org.eclipse.jetty.jsp.JettyJspServlet
2018-04-06 09:46:10.021:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2018-04-06 09:46:10.021:INFO:oejs.session:main: No SessionScavenger set, using defaults
2018-04-06 09:46:10.022:INFO:oejs.session:main: Scavenging every 660000ms
2018-04-06 09:46:10.048:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@1ffaf86{/SwbppServlet-1,file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080-SwbppServlet-1.war-_SwbppServlet-1-any-2425585091937596948.dir/webapp/,AVAILABLE}{/SwbppServlet-1.war}
2018-04-06 09:46:10.114:WARN::main: async-rest webapp is deployed. DO NOT USE IN PRODUCTION!
2018-04-06 09:46:10.115:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2018-04-06 09:46:10.118:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@4f49f6af{/,[file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080-ROOT.war-_-any-10196676678941755136.dir/webapp/, jar:file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080-ROOT.war-_-any-10196676678941755136.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.4.9.v20180320.jar!/META-INF/resources],AVAILABLE}{/ROOT.war}
2018-04-06 09:46:10.155:INFO:oejs.AbstractConnector:main: Started ServerConnector@72ef8d15{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2018-04-06 09:46:10.155:INFO:oejs.Server:main: Started @1082ms

尝试访问 http://127.0.0.1:8080/swbpp 服务器时 returns 404。

但是,我可以访问 / 因为我复制了 ROOT.war,它显示了一个示例 eBay 应用程序。

任何帮助将不胜感激,谢谢!

行...

2018-04-06 09:46:10.118:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@4f49f6af{/,[file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080-ROOT.war--any-10196676678941755136.dir/webapp/, jar:file:///private/var/folders/t3/zbrns2cx3396mjbqhnzwfhz00000gn/T/jetty-0.0.0.0-8080-ROOT.war--any-10196676678941755136.dir/webapp/WEB-INF/lib/example-async-rest-jar-9.4.9.v20180320.jar!/META-INF/resources],AVAILABLE}{/ROOT.war}

表示 webapp 部署在 /contextRoot

因此请尝试 http://127.0.0.1:8080/ 获取上下文的 "base resource"(又名 home/root 位置)(如果您没有 src/main/webapp/index.html 或目录列表,可能会产生 404如果您的环境是这样配置的)

您选择 url-pattern of /swbpp/* 意味着您可以使用以下路径示例来测试该 servlet。

Request URI                       | status | request.getPathInfo()
--------------------------------- | ------ | ---------------------
http://127.0.0.1:8080/swbpp       | 404    | (n/a - see below)
http://127.0.0.1:8080/swbpp/      | 200    | "/"
http://127.0.0.1:8080/swbpp/foo   | 200    | "/foo"
http://127.0.0.1:8080/swbpp/a/b/c | 200    | "/a/b/c"

那是因为 /swbpp 不属于 url-pattern。

如果您有一个名为 src/main/webapp/swbpp 的文件,那么在请求 /swbpp

时将提供该文件的内容

根据之前的回复,我了解到设置的上下文不正确,而不是更改文件的名称,我了解到您可以从此处生成不同的 XML 文件:

https://www.eclipse.org/jetty/documentation/9.4.x/configuring-specific-webapp-deployment.html

所以我将一个 SwbppServlet-1.xml 文件放入 ${JETTY_BASE}/webapps 中,内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/swbpp</Set>
  <Set name="war">/Users/dlynch/Services/Jetty-Base/swbpp/webapps/SwbppServlet-1.war</Set>
  <Set name="extractWAR">false</Set>
</Configure>

那时我得到的是一个目录列表,而不是我的代码 运行。审核后:

Jetty Servlet does not run -- getting directory listing instead

我将 WEB_INF/web.xml 文件更改为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
   metadata-complete="false"
   version="3.1">

  <servlet>
    <servlet-name>swbpp</servlet-name>
    <servlet-class>org.firezeal.SwbppServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>swbpp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

我认为由于上下文已经设置,url 模式应该是 /,这对我有用。