JAVA calls JS functions, and java calls js
Today, js and Java interaction are used in development. In normal times, we usually use JavaScript to call Java methods. dwr, Ajax, and jquery can be used. I suddenly found that I have never met anyone who wants to call js in Java. I checked the information online today and summarized it by the way:
Prerequisites: jdk1.6 is supported, but earlier than 1.6 is not supported.
The js Code is as follows:
function convert (id,str){if ('505041'==id){return str;}else{return "A";}}
The Java code is as follows:
package com.sc;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import javax.script.Invocable;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;public class JsTest {public static void main(String[] args) throws Exception { testJSFile(); } private static void testJSFile() throws Exception { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("javascript"); engine.eval(readJSFile()); Invocable inv = (Invocable) engine; Object res = (Object) inv.invokeFunction("convert", new String[] { "5050412", "D" }); System.out.println("res:" + res); } private static String readJSFile() throws Exception { StringBuffer script = new StringBuffer(); File file = new File("E:\\workspace\\test4\\WebRoot\\test.js"); FileReader filereader = new FileReader(file); BufferedReader bufferreader = new BufferedReader(filereader); String tempString = null; while ((tempString = bufferreader.readLine()) != null) { script.append(tempString).append("\n"); } bufferreader.close(); filereader.close(); return script.toString(); }}
Run in the Java class to get the following results:
Res:
Record this.
Java calls js Functions
Function is the syntax in javascript, while <%> calls the java syntax. Therefore, you cannot call alert () with <%>, because they are not the same thing.
I don't know if you mean to call alert () as soon as the page is loaded. If so, we recommend that you use the onload event to call alert () when the page is loaded ();
Or if it is called when you click, you can call onclick () for a long time. In short, you can use the event to complete the call.
How does java call javascript Functions?
Package com. sinaapp. manjushri;
Import java. io. FileReader;
Import javax. script. Invocable;
Import javax. script. ScriptEngine;
Import javax. script. ScriptEngineManager;
/**
* Call and execute js files in Java, PASS Parameters, and return values of activities
*
* @ 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 the js File
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 input two parameters
// C = merge (2, 3 );
Double c = (Double) invoke. invokeFunction ("merge", 2, 3 );
System. out. println ("c =" + c );
}
Reader. close ();
}
}