Java Reference
In-Depth Information
Changing the Default ScriptContext
You can get and set the default context of a ScriptEngine using its getContext() and
setContext() methods, respectively, as shown:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// Get the default context of the ScriptEngine
ScriptContext defaultCtx = engine.getContext();
// Work with defaultCtx here
// Create a new context
ScriptContext ctx = new SimpleScriptContext();
// Configure ctx here
// Set ctx as the new default context for the engine
engine.setContext(ctx);
Note that setting a new default context for a ScriptEngine will not use the Bindings
of the ScriptEngineManager as its global scope Bindings . If you want the new default
context to use the Bindings of the ScriptEngineManager , you need set it explicitly as
shown:
// Create a new context
ScriptContext ctx = new SimpleScriptContext();
// Set the global scope Bindings for ctx the same as the Bindings for
// the manager
ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
// Set ctx as the new default context for the engine
engine.setContext(ctx);
 
Search WWH ::




Custom Search