【问题标题】:Simple Jetty app built with Maven according to instructions returns 404根据说明使用 Maven 构建的简单 Jetty 应用程序返回 404
【发布时间】:2018-04-06 17:13:52
【问题描述】:

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

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 时,服务器返回 404。

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

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

【问题讨论】:

    标签: maven jetty


    【解决方案1】:

    线...

    2018-04-06 09:46:10.118:INFO:oejsh.ContextHandler:main: 开始 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/ 作为上下文的“基本资源”(也称为主/根位置)(如果您没有 src/main/webapp/index.html 或目录列表,如果您的环境配置为这样,则可能会产生 404)

    您选择的url-pattern/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 模式的一部分。

    如果您有一个名为 src/main/webapp/swbpp 的文件,那么在请求 /swbpp 时将提供该文件的内容

    【讨论】:

      【解决方案2】:

      根据之前的回复,我了解到设置的上下文不正确,而不是更改文件名,我了解到您可以从这里生成不同的 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 模式应该是 /,这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-04
        • 2013-04-30
        • 2019-03-28
        • 2014-07-16
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        相关资源
        最近更新 更多