代码之家  ›  专栏  ›  技术社区  ›  Reinderien

Jetty不加载任何servlet

  •  0
  • Reinderien  · 技术社区  · 6 年前

    我使用的是安装在Ubuntu服务器LTS上的openjre11和jetty9。使用此来源:

    $ find .
    ./build.gradle
    ./gradlew
    ./src
    ./src/main
    ./src/main/java
    ./src/main/java/com.example
    ./src/main/java/com.example/servlet
    ./src/main/java/com.example/servlet/ExampleServlet.java
    ./src/main/webapp
    ./src/main/webapp/WEB-INF
    ./src/main/webapp/WEB-INF/jetty-web.xml
    ./src/main/webapp/WEB-INF/web.xml
    

    构建.gradle:

    plugins {
        id 'java'
        id 'war'
    }
    
    repositories {
        jcenter()
    }
    
    group 'com.example'
    version '0.1'
    
    // For Ubuntu's openjdk-11-jre-headless
    sourceCompatibility = 1.11
    
    dependencies {
        def jsp_dep = 'org.eclipse.jetty:apache-jsp:9.4.15.v20190215'
        if (gradle.startParameter.taskNames.contains('war')) {
            // Bundled in Ubuntu's libjetty9-java so not embedded
            compileOnly jsp_dep
        }
    
        // This should not need to be 'implementation'.
        // When hosted in Jetty, this alone is enough to support JSTL. This is a thin reference to
        // org.apache.taglibs:taglibs-standard-{spec,impl}:1.2.5
        implementation 'org.eclipse.jetty:apache-jstl:9.4.15.v20190215'
    }
    

    示例Servlet:

    package com.example.servlet;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/example")
    public class ExampleServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        }
    }
    

    码头-网站.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- configure-9_4 is missing -->
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
    
    </Configure>
    

    网站.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- https://download.oracle.com/otn-pub/jcp/servlet-4-final-eval-spec/servlet-4_0_FINAL.pdf
    -->
    
    <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_4_0.xsd"
             version="4.0">
    </web-app>
    

    编译和安装:

    $ ./gradlew war
    
    BUILD SUCCESSFUL in 1s
    2 actionable tasks: 2 executed
    
    $ unzip -l build/libs/jetty-example-0.1.war
    Archive:  build/libs/jetty-example-0.1.war
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  2019-06-26 14:08   META-INF/
           25  2019-06-26 14:08   META-INF/MANIFEST.MF
            0  2019-06-26 14:08   WEB-INF/
            0  2019-06-26 14:08   WEB-INF/classes/
            0  2019-06-26 14:08   WEB-INF/classes/com/
            0  2019-06-26 14:08   WEB-INF/classes/com/example/
            0  2019-06-26 14:08   WEB-INF/classes/com/example/servlet/
          696  2019-06-26 14:08   WEB-INF/classes/com/example/servlet/ExampleServlet.class
            0  2019-06-26 14:08   WEB-INF/lib/
        12585  2019-05-04 18:20   WEB-INF/lib/apache-jstl-9.4.15.v20190215.jar
        40153  2019-04-15 11:56   WEB-INF/lib/taglibs-standard-spec-1.2.5.jar
       206430  2019-04-15 11:56   WEB-INF/lib/taglibs-standard-impl-1.2.5.jar
          255  2019-06-26 13:55   WEB-INF/jetty-web.xml
          444  2019-06-26 14:03   WEB-INF/web.xml
    ---------                     -------
       260588                     14 files
    
    $ mv jetty-example-0.1.war root.war
    $ chmod 644 root.war
    # chown jetty:adm root.war
    # mv root.war /var/lib/jetty9/webapps/
    
    # ls -la /tmp/systemd-private-*jetty9.service*/tmp/*root.war*
    total 12
    drwxr-xr-x 3 jetty jetty 4096 Jun 26 14:08 .
    drwxrwxrwt 6 root  root  4096 Jun 26 14:08 ..
    drwxr-xr-x 2 jetty jetty 4096 Jun 26 14:08 jsp
    

    当访问127.0.0.1:8080/example时,我得到了Jetty的默认404页面,这使我相信包扫描在默认情况下不会发生。在/var/log/syslog中,我得到以下消息:

    Jun 26 14:07:07 web jetty9[475]: 2019-06-26 14:07:07.980:INFO:oejshC.root:Scanner-0: Warning: No org.apache.tomcat.JarScanner set in ServletContext. Falling back to default JarScanner implementation.
    Jun 26 14:07:08 web jetty9[475]: 2019-06-26 14:07:08.162:INFO:oajs.TldScanner:Scanner-0: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Jun 26 14:07:08 web jetty9[475]: 2019-06-26 14:07:08.204:INFO:oejsh.ContextHandler:Scanner-0: Started o.e.j.w.WebAppContext@156a87be{root,/,file:///var/lib/jetty9/webapps/root/,AVAILABLE}{/root}
    Jun 26 14:07:08 web jetty9[475]: 2019-06-26 14:07:08.280:INFO:oejsh.ContextHandler:Scanner-0: Stopped o.e.j.w.WebAppContext@7f3b84b8{root,/,null,UNAVAILABLE}{/root.war}
    Jun 26 14:08:29 web jetty9[475]: 2019-06-26 14:08:29.532:INFO:oeja.AnnotationConfiguration:Scanner-0: Scanning elapsed time=28ms
    Jun 26 14:08:29 web jetty9[475]: 2019-06-26 14:08:29.564:INFO:oejshC.root:Scanner-0: Warning: No org.apache.tomcat.JarScanner set in ServletContext. Falling back to default JarScanner implementation.
    Jun 26 14:08:29 web jetty9[475]: 2019-06-26 14:08:29.725:INFO:oajs.TldScanner:Scanner-0: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Jun 26 14:08:29 web jetty9[475]: 2019-06-26 14:08:29.782:INFO:oejsh.ContextHandler:Scanner-0: Started o.e.j.w.WebAppContext@42e87462{root,/,file:///var/lib/jetty9/webapps/root/,AVAILABLE}{/root.war}
    

    为什么默认jar扫描不起作用,如何修复它?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Reinderien    6 年前

    事实证明:被破坏的不是包扫描;而是一个冲突的上下文。不知何故,Jetty认为两个重叠的上下文应该无声地失败。在我的特殊情况下,出现了违约 root 包含Jetty中的示例web应用程序的目录。删除它可以解决问题。

    推荐文章