Friday, September 5, 2025
HomeLanguagesJavaSimpleScriptContext getAttribute() method in Java with Examples

SimpleScriptContext getAttribute() method in Java with Examples

The getAttribute() method of a SimpleScriptContext class is used to return the value of the attribute with the given name as a parameter to the method.The searching the value via the attribute name is in the scope occurring earliest in the search order.

Syntax:

public Object getAttribute(String name)

Parameters: This method accepts a single parameter name which is the name of the attribute to retrieve.

Return value: This method returns the value of the attribute in the lowest scope for which an attribute with the given name is defined and returns null if no attribute with the name exists 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.getAttribute() method:
Program 1:




// Java program to demonstrate
// SimpleScriptContext.getAttribute() method
  
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(
            "name",
            "Value",
            ScriptContext.ENGINE_SCOPE);
  
        // get value using getAttribute()
        Object value
            = simple.getAttribute("name");
  
        // print
        System.out.println(value);
    }
}


Output:

Value

Program 2:




// Java program to demonstrate
// SimpleScriptContext.getAttribute() method
  
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 value using getAttribute()
        Object value1
            = simple.getAttribute("Team1");
        Object value2
            = simple.getAttribute("Team2");
        Object value3
            = simple.getAttribute("Team3");
  
        // print
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
    }
}


Output:

India
Japan
Nepal

References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#getAttribute(java.lang.String)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS