Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...