Template Literals Not Working

If you are using JavaScript’s new template literal syntax in a JSP page it might not work as expected. That’s because JSP and JavaScript both use the ${myVar} syntax.

This will not work as expected:

//my.jsp
<script>
var n = "Dave";
var s = `Hello ${n}`;
</script>

The Fix

The fix is to escape the ${myVar} with a backslash so that JSP ignores it:

//my.jsp
<script>
var n = "Dave";
var s = `Hello \${n}`; //note the backslash
</script>