代码之家  ›  专栏  ›  技术社区  ›  Daniel Lynch

根据说明使用Maven构建的简单Jetty应用程序返回404

  •  0
  • Daniel Lynch  · 技术社区  · 7 年前

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

    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>
    

    /用户/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很好地编译了项目。然后我运行jetty:

    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。

    但是,我可以访问/因为我复制了根目录。war,其中显示了一个示例eBay应用程序。

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

    2 回复  |  直到 7 年前
        1
  •  0
  •   Joakim Erdfelt    7 年前

    该行。。。

    2018-04-06 09:46:10.118:信息: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。目录/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。罐子/META-INF/resources],可用}{/ROOT.war}

    表示webapp部署在 contextRoot 属于 /

    所以试试看 http://127.0.0.1:8080/ 对于上下文的“基本资源”(即主/根位置)(如果没有 src/main/webapp/index.html 或目录列表(如果您的环境是这样配置的)

    您选择的 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
  •  0
  •   Daniel Lynch    7 年前

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

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

    所以我扔了一个SwbppServlet-1。xml文件转换为${JETTY\u 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\u 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模式应该是/,这对我来说很有效。