Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
RhinoPoundToKilo.java - Exchange Data with Rhino Variables
How to exchange data between Java objects and Rhino JavaScript variables?
✍: FYIcenter
Exchanging data between Java objects and Rhino JavaScript variables can be done in 2 ways:
1. Sending data from Java to JavaScript - Call Context.javaToJS() method first to convert a Java object to a Rhino object. Then call ScriptableObject.putProperty() method to assign the Rhino object to JavaScript variable:
Double pound = Double.valueOf(args[0]); Object jsP = Context.javaToJS(pound, s); ScriptableObject.putProperty(s, "p", jsP);
2. Sending data from JavaScript to Java - Call ScriptableObject.setProperty() method first to retrieve the JavaScript variable as a Rhino object. Then call Context.jsToJava() to convert the Rhino object to a Java object:
Object jsK = ScriptableObject.getProperty(s, "k"); Double kilo = (Double) Context.jsToJava(jsK, Double.class);
There is also a quick way to retrieve JavaScript variable by calling s.get() method:
Object jsK = s.get("k", s); double kilo = Context.toNumber(jsK);
Here is the entire example program, RhinoPoundToKilo, that shows how to exchange data between Java objects and Rhino JavaScript variables:
// Copyright (c) 2017 FYIcenter.com import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; public class RhinoPoundToKilo { public static void main(String[] args) throws Exception { ContextFactory f = new ContextFactory(); Context c = f.enterContext(); Scriptable s = c.initStandardObjects(); String js = "function p2k(p) {\n"; js += " return p*0.453592;\n"; js += "}\n"; js += "var k = p2k(p);"; Double pound = Double.valueOf(args[0]); Object jsP = Context.javaToJS(pound, s); ScriptableObject.putProperty(s, "p", jsP); c.evaluateString(s, js, null, 1, null); Object jsK = ScriptableObject.getProperty(s, "k"); Double kilo = (Double) Context.jsToJava(jsK, Double.class); System.out.println("Pound to Kilo conversion:"); System.out.println(" Pound = "+pound); System.out.println(" Kilo = "+kilo); System.out.println("JavaScript used:"); System.out.println(js); } }
Compile and run the example program, RhinoPoundToKilo.java:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\rhino1_7R5\js.jar RhinoPoundToKilo.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\rhino1_7R5\js.jar RhinoPoundToKilo 5.0 Pound to Kilo conversion: Pound = 5.0 Kilo = 2.26796 JavaScript used: function p2k(p) { return p*0.453592; } var k = p2k(p);
⇒ RhinoFunctionCall.java - Call JavaScript Function from Java
⇐ RhinoExportVar.java - Exporting Rhino Variable to Java
2017-08-08, 1891🔥, 0💬
Popular Posts:
What Is poi-ooxml-3.5.jar? poi-ooxml-3.5.jar is one of the JAR files for Apache POI 3.5, which provi...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...
How to download and install javamail-1_2.zip? The JavaMail API is a set of abstract APIs that model ...
What Is javaws.jar in JRE (Java Runtime Environment) 8? javaws.jar in JRE (Java Runtime Environment)...
What Is ojdbc5.jar for Oracle 11g R1? ojdbc5.jar for Oracle 11g R1 is the JAR files of ojdbc.jar, JD...