0

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>
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
vnkid
  • 308
  • 2
  • 11
  • 1
    [Jetty 7 is EOL (End of Life)](https://www.eclipse.org/lists/jetty-announce/msg00069.html), consider upgrading to a version that is current and supported. – Joakim Erdfelt Jan 24 '19 at 16:28
  • 1
    Jetty 9 moved away from Glassfish JSP to Apache Jasper JSP because of too many unmerged/unresolved bugs in the Glassfish implementation. – Joakim Erdfelt Jan 24 '19 at 16:29
  • 1
    @JoakimErdfelt "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. " – eis Jan 24 '19 at 16:29
  • The #1 reason for continuing to use Jetty 7 is Android, if so, you must use `jspc` from jetty and the glassfish implementation from jetty and precompile your JSPs before deployment. Also note that since Jetty 7 is EOL existing/known/public vulnerabilities against it are not fixed. – Joakim Erdfelt Jan 24 '19 at 16:35
  • @JoakimErdfelt yeah, I do know that. This supposed to be just a temporary thing I need for my actual project – vnkid Jan 24 '19 at 16:36
  • @JoakimErdfelt I have question to you. I have downloaded the branch 8.x.x from you on Github and just changed the version in pom.xml from 8.x.x to 7.6.0x. And now it keeps throwing JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application What am I doing wrong here? – vnkid Feb 06 '19 at 13:18

1 Answers1

1

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.

eis
  • 51,991
  • 13
  • 150
  • 199
  • precompile is important here. you'll know if you did it right when the WEB-INF/web.xml has been updated to include static references to new servlets (the results of your precompiled jsps). – Joakim Erdfelt Jan 24 '19 at 16:38
  • He's also missing a [bunch of setup for `JspServlet`](https://github.com/jetty-project/embedded-jetty-jsp/blob/jetty-8/src/main/java/org/eclipse/jetty/demo/Main.java) – Joakim Erdfelt Jan 24 '19 at 16:39
  • @eis I am using the JDK – vnkid Jan 24 '19 at 16:47
  • @vnkid ok, then you at least need to add jsp servlet setup as Joakim linked. But precompile would be preferred. – eis Jan 24 '19 at 17:33