Open In App

SimpleScriptContext removeAttribute() method in Java with Examples

Last Updated : 29 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The removeAttribute() method of a SimpleScriptContext class is used to remove an attribute in a given scope and the name of attribute and scope are passed as parameters.

Syntax:

public Object removeAttribute(String name, int scope)

Parameters: This method accepts two parameters:

  • name which is the Name of the attribute and
  • scope which is the scope in which to remove the attribute.

Return value: This method returns removed value.

Exceptions: This method throws following Exceptions:

  • NullPointerException: if the name is null.
  • IllegalArgumentException: if the name is empty or if the scope is invalid.

Below programs illustrate the SimpleScriptContext.removeAttribute() method:
Program 1:




// Java program to demonstrate
// SimpleScriptContext.removeAttribute() 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);
  
        // remove using removeAttribute()
        Object removedObject
            = simple.removeAttribute(
                "name",
                ScriptContext.ENGINE_SCOPE);
  
        // print
        System.out.println("Removed Object :"
                           + removedObject);
    }
}


Output:

Removed Object :Value

Program 2:




// Java program to demonstrate
// SimpleScriptContext.removeAttribute() 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.GLOBAL_SCOPE);
  
        // remove using removeAttribute()
        Object remove1
            = simple.removeAttribute(
                "Team1",
                ScriptContext.ENGINE_SCOPE);
        Object remove2
            = simple.removeAttribute(
                "Team2",
                ScriptContext.ENGINE_SCOPE);
  
        // print scopes of different teams
        System.out.println("Removed : " + remove1);
        System.out.println("Removed : " + remove2);
    }
}


Output:

Removed : India
Removed : Japan

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads