How to make JSP scriplets on Java 7, Java 8 and above on Apache Sling and Adobe AEM

Sling implementation allow to render JSP with scriplets (of cause we using scriplets instead of Groovy console for hot-fixes). By default JSP compiler allow to write code compatible with Java 6 therefore if on server we has JVM version greater than 6 and desire to write code for recent JVM with additional benefits like Diamond operators from Java 7 or Lambdas and Streams from Java 8 we should configure JSP Scripts Handler to process required Java version otherwise we will see the compilation error:

org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
java.io.IOException: unexpected tag: 18
at org.apache.sling.scripting.jsp.jasper.compiler.SmapUtil$SDEInstaller.copyConstantPool(SmapUtil.java:491)
at org.apache.sling.scripting.jsp.jasper.compiler.SmapUtil$SDEInstaller.addSDE(SmapUtil.java:308)
at org.apache.sling.scripting.jsp.jasper.compiler.SmapUtil$SDEInstaller.<init>(SmapUtil.java:264)
at org.apache.sling.scripting.jsp.jasper.compiler.SmapUtil$SDEInstaller.install(SmapUtil.java:243)
at org.apache.sling.scripting.jsp.jasper.compiler.SmapUtil.installSmap(SmapUtil.java:167)
at org.apache.sling.scripting.jsp.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:151)
at org.apache.sling.scripting.jsp.jasper.compiler.Compiler.compile(Compiler.java:312)
at org.apache.sling.scripting.jsp.jasper.compiler.Compiler.compile(Compiler.java:290)
at org.apache.sling.scripting.jsp.jasper.compiler.Compiler.compile(Compiler.java:277)
at org.apache.sling.scripting.jsp.jasper.JspCompilationContext.compile(JspCompilationContext.java:501)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.prepareServlet(JspServletWrapper.java:427)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:486)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:284)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:102)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:536)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:361)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:171)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:463)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:533)
at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:77)
at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68)
at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:146)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68)
at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:371)
at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:171)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68)
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:282)
at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:77)

So to enable Java 8 within JSP scriplets we should open:

/system/console/configMgr/org.apache.sling.scripting.jsp.JspScriptEngineFactory

and the set Java version to the fields “Target Version” and “Source Version”, in my case I’ve set Java 8:
Configuration example of org.apache.sling.scripting.jsp.JspScriptEngineFactory

Afterwards we can use Java version different from Java 6.

Or we can set Java version directly in config file:

  1. create file org.apache.sling.scripting.jsp.JspScriptEngineFactory.config
  2. set config file content as below:
    jasper.compilerTargetVM="1.8"
    jasper.compilerSourceVM="1.8"