How to Make JSP Scriptlets on Java 7, Java 8 and Above on Apache Sling and Adobe AEM

Evgeniy Fitsner Software Engineer
2 min read
How to Make JSP Scriptlets on Java 7, Java 8 and Above on Apache Sling and Adobe AEM

Introduction

Apache Sling permits rendering JSP files with scriptlets. By default, the JSP compiler targets Java 6 compatibility. When running on newer JVM versions and wanting to use modern Java features like diamond operators (Java 7) or lambdas and streams (Java 8), you must configure the Sling JSP Scripts Handler accordingly.

The Problem

Without proper configuration, you will encounter compilation errors when using newer Java syntax in your JSP scriptlets:

1
2
org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException
java.io.IOException: unexpected tag: 18

This error occurs because the JSP compiler is still targeting Java 6 bytecode, which does not understand the newer class file format generated by Java 7+ language features.

Solution

Method 1: Web Console Configuration

Navigate to the OSGi configuration manager:

1
http://localhost:4502/system/console/configMgr/org.apache.sling.scripting.jsp.JspScriptEngineFactory

Set both Target Version and Source Version fields to your desired Java version (for example, 1.8).

Method 2: Configuration File

Create a configuration file named org.apache.sling.scripting.jsp.JspScriptEngineFactory.config with the following content:

1
2
jasper.compilerTargetVM="1.8"
jasper.compilerSourceVM="1.8"

Place this file in your project’s configuration directory so it gets deployed with your code package.

Verification

After applying either configuration, JSP scriptlets will support modern Java syntax features including:

  • Diamond operators (<>) from Java 7
  • Try-with-resources from Java 7
  • Lambda expressions from Java 8
  • Stream API from Java 8
  • Method references from Java 8

Restart your AEM instance or refresh the OSGi bundle to ensure the new configuration takes effect.

Contents