The getBindings() method of a SimpleScriptContext class is used to return the bindings associated with the given scope in this ScriptContext where scope is passed as parameter.
Syntax:
Bindings getBindings(int scope)
Parameters: This method accepts a single parameter scope which is the scope in this ScriptContext.
Return value: This method returns associated Bindings and returns null if it has not been set.
Exceptions: This method throws following Exceptions:
- IllegalArgumentException: If no Bindings is defined for the specified scope value in ScriptContext of this type.
Below programs illustrate the SimpleScriptContext.getBindings() method:
Program 1:
// Java program to demonstrate // SimpleScriptContext.getBindings() method import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // add some attribute simple.setAttribute( "name1" , "Value1" , ScriptContext.ENGINE_SCOPE); simple.setAttribute( "name2" , "Value2" , ScriptContext.ENGINE_SCOPE); // get bindings using getBindings() Bindings bindings = simple.getBindings( ScriptContext.ENGINE_SCOPE); // print System.out.println( "Bindings value for name1:" + bindings.get( "name1" )); System.out.println( "Bindings value for name2:" + bindings.get( "name2" )); } } |
Bindings value for name1:Value1 Bindings value for name2:Value2
Program 2:
// Java program to demonstrate // SimpleScriptContext.getBindings() method import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // add some attribute simple.setAttribute( "Team1" , "India" , ScriptContext.ENGINE_SCOPE); simple.setAttribute( "Team2" , "Japan" , ScriptContext.ENGINE_SCOPE); simple.setAttribute( "Team3" , "Nepal" , ScriptContext.ENGINE_SCOPE); // get bindings using getBindings() Bindings bindings = simple.getBindings( ScriptContext.ENGINE_SCOPE); // print System.out.println( "Bindings value for Team1:" + bindings.get( "Team1" )); System.out.println( "Bindings value for Team2:" + bindings.get( "Team2" )); System.out.println( "Bindings value for Team3:" + bindings.get( "Team3" )); } } |
Bindings value for Team1:India Bindings value for Team2:Japan Bindings value for Team3:Nepal
References: https://docs.oracle.com/javase/10/docs/api/javax/script/ScriptContext.html#getBindings(int)