0

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

fglez
  • 8,422
  • 4
  • 47
  • 78
Varun
  • 5,001
  • 12
  • 54
  • 85
  • have to do some server side processing... but the processing is already done using a javascript so want to implement the same script for the work... – Varun Dec 03 '10 at 11:09

2 Answers2

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;

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();
        }    
    }
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • cannot understand just going through the same... going over the head can any1 show me a working example with a javascript and the corresponding java file.. – Varun Dec 03 '10 at 10:38
  • Sure you can. Look at the examples at the pages I linked to. – aioobe Dec 03 '10 at 10:59
  • i am getting error on trying the example given above... do we need to setup some environment??? – Varun Dec 03 '10 at 11:07
  • i dont want java6 example have to implement by java5 only.. – Varun Dec 06 '10 at 12:12
0

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

Atmocreations
  • 9,923
  • 15
  • 67
  • 102