Skip to content Skip to sidebar Skip to footer

Calling Some Javascript Method From A Java Class

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???

Solution 1:

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;

publicclassRhinoEngine {
    publicstaticvoidmain(String[] args) {

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

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

Solution 2:

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

Post a Comment for "Calling Some Javascript Method From A Java Class"