can anyone give a complete example of how to call a js using java file?
My code is like
SJCL.js
function encrypt(data, key){
......
}
abcd.java
public String callJavascript(String data,String key)
{
// i want to call开发者_运维技巧 the encrypt method here with data,key value passing to it
}
any help????
using java5 only
I'm sure you checked this one: http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
What you are probably looking for is under the 'Invoking Script Functions and Methods' (an example to call your 'crypt()' from java)
ok, just a thought on feasibility though: you can always pass a 'java.io.Reader' pointing your js file, to engine.eval() but if this is a web application then you're heading for disaster. You'd be better off keeping the encrypt() functionality from sjcl.js in a separate file (say encrypt.js), including this file into sjcl.js.
You can then read encrypt.js once and cache its contents in a static String in your java class. You can then pass this string to engine.eval() without I/O performance impact.
I think following articles will help you:
- Java to JavaScript Communication
- How Java to Javascript Communication Works in Java Plug-in
EDIT:
Rhino:
I think reading the documentation will help you to implement this.
- Rhino Documentation
- Rhino (JavaScript engine)
- Tutorial
There are also some other options:
- JavaScript Engines
精彩评论