-
Notifications
You must be signed in to change notification settings - Fork 0
Defining Local Variables in a Script
pranithcodes edited this page Apr 6, 2018
·
1 revision
In the previous example of the while loop, the variable “i” is used for iterating the count and so, we don’t need to define it in the context. However, if we try the same example, by removing “i” from the context and the code will throw an exeception.
Var can be used to initialise the variable that are using within the scirpt public static void main(String[] args) throws ScriptException {
String script = "var i = 0; while(i < 10 ){ i = i+1; out.println(i)}";
JexlScriptEngine jexlScriptEngine = new JexlScriptEngine();
Bindings bindings = new SimpleBindings();
bindings.put("out", System.out);
jexlScriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
jexlScriptEngine.eval(script);
}
In this example, we have removed “i” from the script context and defined it as local variable of the script using var keyword(var i = 0;).
Similarly, we can define String and other datatypes.