Java Reference
In-Depth Information
■
By default, all scriptengines that a
ScriptEngineManager
creates share its
Bindings
as their global scope
Bindings
. if you use the
setBindings()
method of a
ScriptEngine
to set its global scope
Bindings
or if you use the
setBindings()
method of
a
ScriptEngineManager
to set its
Bindings
, you break the “globalness” chain as discussed
in this section. to keep the “globalness” chain intact, you should always use the
put()
method of the
ScriptEngineManager
to add key-value pairs to its
Bindings
. to remove a
key-value pair from the global scope of all scriptengines created by a
ScriptEngineManager
,
you need to get the reference of the
Bindings
using the
getBindings()
method of the
ScriptEngineManager
and use the
remove()
method on the
Bindings
.
Tip
Using a Custom ScriptContext
In the previous section, you saw that each
ScriptEngine
has a default script context. The
get()
,
put()
,
getBindings()
, and
setBindings()
methods of the
ScriptEngine
operate
on its default
ScriptContext
. When no
ScriptContext
is specified to the
eval()
method
of the
ScriptEngine
, the default context of the engine is used. The following two versions
of the
eval()
method of the
ScriptEngine
use its default context to execute the script:
•
Object eval(String script)
Object eval(Reader reader)
You can pass a
Bindings
to the following two versions of the
eval()
method:
•
•
Object eval(String script, Bindings bindings)
Object eval(Reader reader, Bindings bindings)
These versions of the
eval()
method do not use the default context of the
ScriptEngine
. They use a new
ScriptContext
whose engine scope
Bindings
is the one
passed to these methods and the global scope
Bindings
is the same as for the default
context of the engine. Note that these two versions of the
eval()
method keep the default
context of the
ScriptEngine
untouched.
You can pass a
ScriptContext
to the following two versions of the
eval()
method:
•
•
Object eval(String script, ScriptContext context)
Object eval(Reader reader, ScriptContext context)
These versions of the
eval()
method use the specified context to execute the script.
They keep the default context of the
ScriptEngine
untouched.
•
Search WWH ::
Custom Search