Jetty 7.6 does not compile JSP files

I am new with Jetty and JSP. I am now trying to create simple server with Embedded Jetty and JSP for the html generating.

What I should have to mention first, that I am restricted with the Jetty version. The version I have to use is Jetty 7.6.x.x.

My need is to create few servlets, where I can dispatch the request/response to the JSP file. The thing is that the JSP file does not seem to be compiled and instead of evaluating expressions, it throws the whole script as a plain text in browser. Let’s have a look.

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();

}

This is the JSP file

    <!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>

And this is the result:

enter image description here

I am not using web.xml, but I won’t mind using it if it will fix my problem.

Also here is my maven dependencies:

<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>

Answer

From 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.

This sounds exactly like your issue. Either use a JDK or precompile your JSPs, as instructed by the link above.

Leave a Reply

Your email address will not be published. Required fields are marked *