Sundararajan's Weblog

  • Java
    October 11, 2006

JSR-223 script engine for JUEL

Guest Author

Just added JSR-223 script engine for JUEL - Java Unified Expression Language - which is an implementation of the Unified Expression Language (EL), specified as a part of the JSP 2.1 standard (JSR-245).

EL started as part of JSTL. Then, the EL moved into the JSP 2.0 spec. Now, although EL is part of JSP 2.1, the EL has been separated into package javax.el and all dependencies to the core JSP classes have been removed. So, the EL can be used by non-JSP applications as well. With this new JSR-223 script engine, it is possible to use EL through javax.script API. As usual, the script engine sources and binaries are available at scripting.dev.java.net.

Sample Program

import javax.script.\*;
public class Main {
public static void main(String[] a) throws ScriptException {ScriptEngineManager manager = new ScriptEngineManager();ScriptEngine e = manager.getEngineByName("juel");
// expose PI
e.put("PI", Math.PI);
// prints false
System.out.println(e.eval("${222 < 32}"));
// context is pre-defined variable of type
// javax.script.ScriptContext
// print "hello world"
e.eval("${out:print('hello world')}");
// lang:import function imports all static methods
// of the given class.
// import all static functions in java.lang.Math
e.eval("${lang:import(context, 'Math', 'java.lang.Math')}");

Compiling the above code is straightforward. While running it, you need to put the
JUEL jar and the jsr-223 script engine jar in the CLASSPATH. For example:

D:\\scripting\\engines\\juel\\bin>java -cp D:\\scripting\\engines\\juel\\lib\\juel-2.1.0-rc2.jar;D:\\scripting\\engines\\juel\\build\\juel-engine.jar;. Main
hello world

If you are looking for a lightweight, expression-only language (rather than
a full-blown scripting language), you have (atleast) 4 different options:

  1. JEP (Java Math Expression Parser)
  2. JEXL (Java Expression Language)
  3. JUEP (Java Unified Expression Language)
  4. OGNL (Object Graph Navigation Language)

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.