开发者

Calling some javascript method from a java class [closed]

开发者 https://www.devze.com 2023-01-28 12:33 出处:网络
It's difficult to tell what开发者_如何学运维 is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
It's difficult to tell what开发者_如何学运维 is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

i want to call a javascript method from a servlet... is it possible??

i have heard of something called mozila rhino but cannot understand its use, do any 1 has any idea???


I want to call a javascript method from a servlet... is it possible??

Yes, have a look at the Rhino tutorial. It has a few nice examples of how to embed the execution of JavaScript in a Java application.

You may also want to have a look at the example on the Rhino article on Wikipedia. I'll paste it here for reference:

Below is an example of Java code running JavaScript print('Hello, world!')

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class RhinoEngine {
    public static void main(String[] args) {

        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");

        try {
            engine.put("name", args[0]);
            engine.eval("print('Hello ' + name + '!')");
        } catch (ScriptException ex) {
            ex.printStackTrace();
        }    
    }
}


You could simply put a <script>-tag onto the website, which will then be executed.

0

精彩评论

暂无评论...
验证码 换一张
取 消