SimpleScriptContext getAttributesScope() method in Java with Examples
Last Updated :
29 Jan, 2020
The getAttributesScope() method of a SimpleScriptContext class is used to return the scope in which an attribute is defined and the name of attribute is passed as parameter.
Syntax:
public int getAttributesScope(String name)
Parameters: This method accepts a single parameter name which is the Name of the attribute.
Return value: This method returns lowest scope and returns -1 if no attribute with the given name is defined in any scope.
Exceptions: This method throws following Exceptions:
- NullPointerException if the name is null.
- IllegalArgumentException if the name is empty.
Below programs illustrate the SimpleScriptContext.getAttributesScope() method:
Program 1:
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
public class GFG {
public static void main(String[] args)
{
SimpleScriptContext simple
= new SimpleScriptContext();
simple.setAttribute(
"name" ,
"Value" ,
ScriptContext.ENGINE_SCOPE);
int scope = simple.getAttributesScope( "name" );
System.out.println( "Scope :" + scope);
}
}
|
Program 2:
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
public class GFG {
public static void main(String[] args)
{
SimpleScriptContext simple
= new SimpleScriptContext();
simple.setAttribute(
"Team1" ,
"India" ,
ScriptContext.ENGINE_SCOPE);
simple.setAttribute(
"Team2" ,
"Japan" ,
ScriptContext.GLOBAL_SCOPE);
simple.setAttribute(
"Team3" ,
"Nepal" ,
ScriptContext.GLOBAL_SCOPE);
int scope1
= simple.getAttributesScope( "Team1" );
int scope2
= simple.getAttributesScope( "Team2" );
int scope3
= simple.getAttributesScope( "Team3" );
System.out.println( "Scope for Team1: "
+ scope1);
System.out.println( "Scope for Team2: "
+ scope2);
System.out.println( "Scope for Team3: "
+ scope3);
}
}
|
Output:
Scope for Team1: 100
Scope for Team2: -1
Scope for Team3: -1
References: https://docs.oracle.com/javase/10/docs/api/javax/script/ScriptContext.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...