The setBindings() method of a SimpleScriptContext class is used to set a Bindings of attributes for the given scope where Bindings and scope are passed as parameters to the method. If the scope is ENGINE_SCOPE the given Bindings replaces the engineScope field. If the scope is GLOBAL_SCOPE the given Bindings replaces the globalScope field.
Syntax:
public void setBindings(Bindings bindings, int scope)
Parameters: This method accepts two-parameters:
- bindings which is the Bindings of attributes to set and
- scope which is the scope in which to set the attribute.
Return value: This method returns nothing.
Exceptions: This method throws following Exceptions:
- NullPointerException: if the value of scope is ENGINE_SCOPE and the specified Bindings is null.
- IllegalArgumentException: if the scope is invalid.
Below programs illustrate the SimpleScriptContext.setBindings() method:
Program 1:
// Java program to demonstrate // SimpleScriptContext.setBindings() method import javax.script.ScriptContext; import javax.script.SimpleBindings; import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // create Bindings SimpleBindings bindings = new SimpleBindings(); // add some key-value to bindings bindings.put( "name1" , "Value1" ); // add bindings to SimpleScriptContext using // setBindings() Method simple.setBindings( bindings, ScriptContext.ENGINE_SCOPE); // print System.out.println( "name1:" + simple.getAttribute( "name1" )); } } |
name1:Value1
Program 2:
// Java program to demonstrate // SimpleScriptContext.setBindings() method import javax.script.ScriptContext; import javax.script.SimpleBindings; import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // create Bindings SimpleBindings bindings = new SimpleBindings(); // add some key-value to bindings bindings.put( "Team1" , "India" ); bindings.put( "Team2" , "Japan" ); bindings.put( "Team3" , "Nepal" ); // add bindings to SimpleScriptContext using // setBindings() Method simple.setBindings( bindings, ScriptContext.ENGINE_SCOPE); // print System.out.println( "Team1:" + simple.getAttribute( "Team1" )); System.out.println( "Team2:" + simple.getAttribute( "Team2" )); System.out.println( "Team3:" + simple.getAttribute( "Team3" )); } } |
Team1:India Team2:Japan Team3:Nepal