The JDK1.6 version adds a new ScriptEngine class that allows the user to execute the JS code directly.
call JS code directly in Java
The JS function defined in the browser cannot be invoked, throwing an exception prompt referenceerror: "Alert" is not defined.
Package Com.sinaapp.manjushri;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException; /** * Directly call JS code
/public class Scriptenginetest {public
static void Main (string[] args) {
Scriptenginemanager manager = new Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("javascript");
try {
engine.eval ("var a=3; var b=4;print (a+b); ");
Can not call the browser defined JS function
//Engine.eval ("alert (\" JS alert\);
Error, the alert reference does not exist exception
} catch (Scriptexception e) {
e.printstacktrace ();
}
}}
Output Result: 7 Binding js variable in Java
Returns NULL if key is not defined when calling Engine.get (key);
Package Com.sinaapp.manjushri;
Import javax.script.Bindings;
Import Javax.script.ScriptContext;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException; public class ScriptEngineTest2 {public static void main (string[] args) {Scriptenginemanager manager = NE
W Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("javascript"); Engine.put ("A", 4);
Engine.put ("B", 3);
Bindings bindings = engine.getbindings (Scriptcontext.engine_scope);
try {//Only is double, and using float and integer throws an exception double result = (double) engine.eval ("A+b");
SYSTEM.OUT.PRINTLN ("result =" + result);
Engine.eval ("C=a+b");
Double c = (double) engine.get ("C");
System.out.println ("c =" + C);
catch (Scriptexception e) {e.printstacktrace ();
}
}}
Output:
result = 7.0
c = 7.0
call the function in the JS file in Java, pass in the call parameter, and get the return value
The merge function in the JS file adds the two parameter a,b and returns C.
Expression.js
function Merge (A, b) {
c = a * b;
return c;
}
Read the JS file in Java code, and parameter two parameters, and then go back to return the value
package com.sinaapp.manjushri; import java.io.FileReader; import
javax.script.Invocable;
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; /** * java calls and executes JS files, passing parameters, and activity return value * * @author manjushri */ public class scriptenginetest { public static void main ( String[] args) throws Exception {
scriptenginemanager manager = new scriptenginemanager (); scriptengine engine = manager.getenginebyname (" JavaScript "); String jsfilename = "Expression.js"; // Read JS files
filereader reader = new filereader (JsFileName); // execute the specified script engine.eval (reader); if ( engine instanceof invocable) { Invocable invoke = (invocable) engine; // Call the merge method and pass in two parameters // c = merge (2, 3); double c = (Double) invoke.invokefunction ("merge", 2, 3); system.out.println ("c = " + c); } Reader.close (); } }
Output results:
c = 5.0