Jetty不加载任何servlet

时间:2019-06-26 18:36:20

标签: servlets jetty jetty-9

我正在使用安装在Ubuntu Server LTS上的OpenJRE 11和Jetty 9。使用此来源:

$ 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

build.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'
}

ExampleServlet:

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) {
    }
}

jetty-web.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>

web.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扫描不起作用,该如何解决?

1 个答案:

答案 0 :(得分:0)

事实证明:不是包扫描损坏了。那是一个矛盾的环境。 Jetty不知何故认为两个重叠的上下文应该静默失败。在我的特定情况下,Jetty中有一个默认的root目录,其中包含示例Web应用程序。删除即可解决问题。