How do I run Java 8 lambda in JSP files

Viewed 405

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

1 Answers

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)

Related