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.
精彩评论