当它在服务器端时,请使用Web服务-可能是带有JSON的RESTful。
- 创建一个Web服务(例如,使用Tomcat)
- 从Javascript调用其URL(例如,使用JQuery或dojo)
当Java代码位于applet中时,你可以使用Javascript桥。Java和Javascript编程语言之间的桥梁(非正式地称为LiveConnect)是在Java插件中实现的。现在,在所有浏览器中都可以使用以前专用于Mozilla的LiveConnect功能,例如调用静态Java方法,实例化新Java对象和从Javascript引用第三方程序包的功能。
以下是文档中的示例。看看
methodReturningString。
Java代码:
public class MethodInvocation extends Applet { public void noArgMethod() { ... } public void someMethod(String arg) { ... } public void someMethod(int arg) { ... } public int methodReturningInt() { return 5; } public String methodReturningString() { return "Hello"; } public OtherClass methodReturningObject() { return new OtherClass(); }}public class OtherClass { public void anotherMethod();}网页和Javascript代码:
<applet id="app" archive="examples.jar" pre="MethodInvocation" ...></applet><script language="javascript"> app.noArgMethod(); app.someMethod("Hello"); app.someMethod(5); var five = app.methodReturningInt(); var hello = app.methodReturningString(); app.methodReturningObject().anotherMethod();</script>


