2

This is a Java JSP project with Maven management, and running Jetty locally.

I have a problem where I get this error when trying to run lambda in .jspf files: Lambda expressions are allowed only at source level 1.8 or above.

My setup:

  • In project structure in Intellij IDEA I have set Project SDK and Project language to Java 8.
  • Module language level is 8 - Lambdas, type annotations etc.
  • Using <maven.compiler.source>1.8</maven.compiler.source> and <maven.compiler.target>1.8</maven.compiler.target>
  • Project bytecode version is set to 1.8
  • Latest version of Jetty (v9.4.31.x)
  • The project is using jetty-jspc-maven-plugin plugin where I have set sourceVersion and targetVersion to 1.8 in <configuration>

Is there anything I'm missing, or is Java 8 simply not possible with JSP / jspc

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Burry
  • 151
  • 3
  • 10
  • 1
    Does this answer your question? [Lambda expressions in JSP files will not compile](https://stackoverflow.com/questions/40035001/lambda-expressions-in-jsp-files-will-not-compile) – Fullslack Sep 25 '20 at 11:09
  • You'll need to configure the JettyJspServlet for compilerSourceVM and compilerTargetVM for a newer JVM (default is 1.5). Are you running Jetty in standalone or embedded mode? (asking as the techniques to configure are different, one is done on filesystem, other is done in code) – Joakim Erdfelt Sep 25 '20 at 11:59
  • @JoakimErdfelt Its in embedded mode.Would like to know how to set it up :) – Burry Sep 25 '20 at 12:25

1 Answers1

1

The existing https://github.com/jetty-project/embedded-jetty-jsp has an example of setting up the compilerSourceVM and compilerTargetVM to allow your *.jsp files to use Java 8 features, like lambda's.

Basically you configure your JettyJspServlet

// Create / Register JSP Servlet (must be named "jsp" per spec)
ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
holderJsp.setInitOrder(0);
holderJsp.setInitParameter("logVerbosityLevel", "DEBUG");
holderJsp.setInitParameter("fork", "false");
holderJsp.setInitParameter("xpoweredBy", "false");
holderJsp.setInitParameter("compilerTargetVM", "1.8");
holderJsp.setInitParameter("compilerSourceVM", "1.8");
holderJsp.setInitParameter("keepgenerated", "true");
servletContextHandler.addServlet(holderJsp, "*.jsp");

I went ahead and added a lambda.jsp that uses a simple lambda within a JSP to prove it out. (lambda's mixed with jsp's JspWriter is rather complicated, it wasn't the best choice of example in hind-sight)

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136