Jetty 服务器不适用于 War 的 JSP

Jetty Server not Working for War with JSPs

我正在尝试创建一个简单的码头 server/container,它将采用 war 文件并进行部署。这不是带有 spring-boot.

的嵌入式码头

这是我的 build.gradle 依赖项:

dependencies {
    def jettyVersion = "9.4.34.v20201102"

    implementation "org.eclipse.jetty:jetty-server:$jettyVersion"
    implementation "org.eclipse.jetty:jetty-security:$jettyVersion"
    implementation "org.eclipse.jetty:jetty-servlet:$jettyVersion"
    implementation "org.eclipse.jetty:jetty-webapp:$jettyVersion"
    implementation "org.eclipse.jetty:jetty-annotations:$jettyVersion"
    implementation "org.eclipse.jetty:jetty-jmx:$jettyVersion"
}

这是我的主要内容 class:

public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        MBeanContainer mbContainer = new MBeanContainer(getPlatformMBeanServer());
        server.addBean(mbContainer);

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");

        webapp.setWar(warFile()); // LOGIC TO UPLOAD WAR FILE
        webapp.setExtractWAR(true);

        Configuration.ClassList classList = Configuration.ClassList.setServerDefault(server);
        classList.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
                "org.eclipse.jetty.annotations.AnnotationConfiguration");

        webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                ".*/[^/]*servlet-api-[^/]*\.jar$|.*/javax.servlet.jsp.jstl-.*\.jar$|.*/[^/]*taglibs.*\.jar$");

        server.setHandler(webapp);
        server.start();
        server.dumpStdErr();
        server.join();
    }

但是,当我尝试访问应用程序 (http://localhost:8080/index) 时,我不断收到以下错误消息:

URI:    /index
STATUS: 500
MESSAGE:    JSP support not configured
SERVLET:    jsp

控制台只有一行错误信息:

2020-12-11 09:49:51.563:INFO:oejshC.ROOT:qtp2025864991-33: No JSP support.  Check that JSP jars are in lib/jsp and that the JSP option has been specified to start.jar

它指的是什么 JSP 罐子?我不知道我需要添加哪些依赖项才能使其适用于 JSPs.

谢谢。

您必须添加 apache-jsp 以便您的服务器支持 jsps。如果您的网络应用程序使用 jstl,您还应该添加 apache-jstl。

对于 WebAppContext 用法(比 ServletContextHandler 用法更容易设置)您将需要以下工件...

确保您的 org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern 可以正确引用 apache-jsp 工件,否则内部 javax.servlet.ServletContextInitializer 将无法正确加载。

如果简单地添加这些工件没有任何反应,您将需要验证 WebAppContext.setDefaultsDescriptor(String) 上的默认描述符设置,以确保将资源引用(路径或 uri)传递给 Jetty 默认描述符XML.

https://github.com/eclipse/jetty.project/blob/jetty-9.4.35.v20201120/jetty-webapp/src/main/config/etc/webdefault.xml

如果您使用 ServletContextHandler 而不是 WebAppContext,那么在嵌入式模式中启用 JSP 支持可能会非常棘手。

如果您决定使用 ServletContextHandler 而不是 WebAppContext(拥有一个 fat/uber jar,加快 load/deploy 时间,简化单元测试等...),然后在 ...

查看 Jetty 维护的示例项目

https://github.com/jetty-project/embedded-jetty-jsp