【问题标题】:Jetty Server not Working for War with JSPsJetty 服务器无法与 JSP 发生战争
【发布时间】:2020-12-11 16:56:20
【问题描述】:

我正在尝试创建一个简单的码头服务器/容器,它将获取一个战争文件并进行部署。这不是带有 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"
}

这是我的主要课程:

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 罐子?我不知道需要添加哪些依赖项才能使其适用于 JSP。

谢谢。

【问题讨论】:

    标签: jetty war


    【解决方案1】:

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

    【讨论】:

    • 这是一个不完整的答案,仅工件只会为您提供类,嵌入式码头代码中仍有大量设置/初始化/配置要在嵌入式码头上成功使用 JSP。
    • OP 提到它不是关于嵌入式码头,而是一个单独的码头服务器应用程序。此外,OP 已将配置 (webapp.setAttribute) 设置为包含 servlet-api 和 servlet-api jar,但在他的类路径中没有它们。这可能无法完全解决问题,但提供了一个良好的起点。
    • 谢谢各位。添加两个依赖项org.eclipse.jetty:apache-jsporg.eclipse.jetty:apache-jstl,我的应用程序现在按预期工作。我可以查看该页面,并且没有任何错误消息。
    • 这不应该与您的问题中的org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern 一起使用。内部的 ServletContextInitializer 不会被找到并正确加载。我敢打赌你现在有一个半功能环境。欺骗性地为您有限的测试用例工作。
    • 它在我的 Web 应用程序中按预期工作,无需添加任何其他依赖项或配置。这个只有一个类和main方法,我上传的war使用的是spring的WebApplicationInitializer(不是springboot)。您的答案确实比简单地添加依赖项包含更多信息,但它们不适用于我的情况。再次感谢您的帮助,祝您有美好的一天!
    【解决方案2】:

    对于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、加快加载/部署时间、简化单元测试等...),然后检查Jetty 维护的示例项目位于 ...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      相关资源
      最近更新 更多