JAVA calls JS functions, and java calls js

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.