SimpleScriptContext setBindings() method in Java with Examples
Last Updated :
29 Jan, 2020
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:
import javax.script.ScriptContext;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
public class GFG {
public static void main(String[] args)
{
SimpleScriptContext simple
= new SimpleScriptContext();
SimpleBindings bindings
= new SimpleBindings();
bindings.put( "name1" , "Value1" );
simple.setBindings(
bindings,
ScriptContext.ENGINE_SCOPE);
System.out.println(
"name1:"
+ simple.getAttribute( "name1" ));
}
}
|
Program 2:
import javax.script.ScriptContext;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
public class GFG {
public static void main(String[] args)
{
SimpleScriptContext simple
= new SimpleScriptContext();
SimpleBindings bindings
= new SimpleBindings();
bindings.put( "Team1" , "India" );
bindings.put( "Team2" , "Japan" );
bindings.put( "Team3" , "Nepal" );
simple.setBindings(
bindings,
ScriptContext.ENGINE_SCOPE);
System.out.println( "Team1:"
+ simple.getAttribute( "Team1" ));
System.out.println( "Team2:"
+ simple.getAttribute( "Team2" ));
System.out.println( "Team3:"
+ simple.getAttribute( "Team3" ));
}
}
|
Output:
Team1:India
Team2:Japan
Team3:Nepal
References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#setBindings(javax.script.Bindings, int)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...