Jetty 7.6 不编译 JSP 个文件

Jetty 7.6 does not compile JSP files

我是 Jetty 和 JSP 的新手。我现在正在尝试使用嵌入式 Jetty 和 JSP 为 html 生成器创建简单的服务器。

我首先要提到的是,我受 Jetty 版本的限制。我必须使用的版本是 Jetty 7.6.x.x。

我需要创建几个 servlet,我可以在其中将 request/response 分派到 JSP 文件。问题是 JSP 文件似乎没有被编译,而不是评估表达式,它在浏览器中将整个脚本作为纯文本抛出。一起来看看吧

public void start() throws Exception {
    server = new Server();
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(port);
    server.addConnector(connector);

    // Base URI to webapp, where jsp files are located
    URI baseUri = getWebRootResourceUri();

    // Create Servlet context
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase(baseUri.toASCIIString());

    // Default Servlet (always last, always named "default")
    ServletHolder holderDefault = new ServletHolder("default", DefaultServlet.class);
    holderDefault.setInitParameter("resourceBase", baseUri.toASCIIString());
    holderDefault.setInitParameter("dirAllowed", "true");
    context.addServlet(holderDefault, "/");
    server.setHandler(context);

    server.start();

}

这是 JSP 文件

    <!DOCTYPE html>
<html>
    <head>
        <title>Coin Flipper</title>
    </head>
    <body>
        <h1>Coin Flipper</h1>
        <p>Flipping a coin...</p>
        <% if(Math.random() < .5){ %>
            <p>Heads!</p>
        <% }
        else{ %>
            <p>Tails!</p>
        <% } %>
        <hr />
        <p>Refresh to flip again.</p>
    </body>
</html>

这是结果:

我没有使用 web.xml,但如果它能解决我的问题,我不介意使用它。

这也是我的 Maven 依赖项:

<dependencies>
        <!-- Embedded web server -->
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>7.6.21.v20160908</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-servlet -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>7.6.21.v20160908</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.glassfish.web/jsp-impl -->
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jsp-impl</artifactId>
            <version>2.1.3-b10</version>
        </dependency>

    </dependencies>

来自https://wiki.eclipse.org/Jetty/Howto/Configure_JSP

In versions of Jetty prior to 7.5.0, the JSP infrastructure made use of the Eclipse Java Compiler (ecj.jar) which is supplied in $JETTY_HOME/lib/jsp. For jetty-7.5.0 we upgraded the version of JSP to jsp-impl-2.1.3.b10 (from Glassfish). In this version, the JSP infrastructure ALWAYS tries to acquire a Java compiler from the JVM if the version of Java is 1.6 or above. Therefore, if you are using a JRE, JSPs are unable to compile so you must use a full JDK. Alternatively, you can precompile your JSPs (which is preferable in a production deployment in any case). The Jetty JSPC Maven Plugin is helpful for precompilation.

这听起来很像你的问题。按照上面 link 的指示,使用 JDK 或预编译您的 JSP。