从 JavaScript 代码调用 Applet 方法
原文: https://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html
网页上的 JavaScript 代码可以与页面上嵌入的 Java 小程序进行交互。 JavaScript 代码可以执行以下操作:
- 在 Java 对象上调用方法
- 获取并设置 Java 对象中的字段
- 获取并设置 Java 数组元素
LiveConnect 规范描述了 JavaScript 代码如何与 Java 代码通信的详细信息。
当 JavaScript 代码调用 Java applet 时,会显示安全警告。要禁止显示这些警告,请将Caller-Allowable-Codebase
属性添加到 JAR 文件清单中。指定允许调用 applet 的 JavaScript 代码的位置。有关Caller-Allowable-Codebase
属性的信息,请参见 JAR 文件清单属性安全。
本主题使用 Math applet 示例探讨 Java applet 通信的 JavaScript 代码。 MathApplet
类和支持Calculator
类提供了一组公共方法和变量。 Web 页面上的 JavaScript 代码调用并评估这些公共成员以传递数据并检索计算结果。
数学小程序和相关类
这是 MathApplet
类的源代码。 getCalculator
方法返回对Calculator
辅助类的引用。
package jstojava;
import java.applet.Applet;
public class MathApplet extends Applet{
public String userName = null;
public String getGreeting() {
return "Hello " + userName;
}
public Calculator getCalculator() {
return new Calculator();
}
public DateHelper getDateHelper() {
return new DateHelper();
}
public void printOut(String text) {
System.out.println(text);
}
}
Calculator
类中的方法允许用户设置两个值,添加数字,并检索范围内的数字。
package jstojava;
public class Calculator {
private int a = 0;
private int b = 0; // assume b > a
public void setNums(int numA, int numB) {
a = numA;
b = numB;
}
public int add() {
return a + b;
}
public int [] getNumInRange() {
int x = a;
int len = (b - a) + 1;
int [] range = new int [len];
for (int i = 0; i < len; i++) {
range[i]= x++;
System.out.println("i: " + i + " ; range[i]: " + range[i]);
}
return range;
}
}
DateHelper
类的getDate
方法返回当前日期。
package jstojava;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateHelper {
public static String label = null;
public String getDate() {
return label + " " + new SimpleDateFormat().format(new Date());
}
}
部署 Applet
在网页中部署 applet, AppletPage.html
部署 applet 时,请确保为 applet 指定 id。稍后使用 applet id 来获取对 applet 对象的引用。
<script src=
"https://www.java.com/js/deployJava.js"></script>
<script>
<!-- applet id can be used to get a reference to
the applet object -->
var attributes = { id:'mathApplet',
code:'jstojava.MathApplet', width:1, height:1} ;
var parameters = { jnlp_href: 'math_applet.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
接下来,将一些 JavaScript 代码添加到 AppletPage.html
网页。 JavaScript 代码可以使用 applet id 作为 applet 对象的引用,并调用 applet 的方法。在下面显示的示例中,JavaScript 代码设置 applet 的公共成员变量,调用公共方法,并检索对 applet 引用的另一个对象的引用(Calculator
)。 JavaScript 代码能够处理原始,数组和对象返回类型。
<script language="javascript">
function enterNums(){
var numA = prompt('Enter number \'a\'?','0');
var numB = prompt(
'Enter number \'b\' (should be greater than number \'a\' ?','1');
// set applet's public variable
mathApplet.userName = "John Doe";
// invoke public applet method
var greeting = mathApplet.getGreeting();
// get another class referenced by applet and
// invoke its methods
var calculator = mathApplet.getCalculator();
calculator.setNums(numA, numB);
// primitive datatype returned by applet
var sum = calculator.add();
// array returned by applet
var numRange = calculator.getNumInRange();
// check Java console log for this message
mathApplet.printOut("Testing printing to System.out");
// get another class, set static field and invoke its methods
var dateHelper = mathApplet.getDateHelper();
dateHelper.label = "Today\'s date is: ";
var dateStr = dateHelper.getDate();
<!-- ... -->
</script>
当数字 a = 0 且 b = 5 时,Math 小程序在网页上显示以下结果:
Results of JavaScript to Java Communication
Hello John Doe
a = 0 ; b = 5
Sum: 5
Numbers in range array: [ 0, 1, 2, 3, 4, 5 ]
Today's date is: 5/28/13 4:12 PM //shows current date
在浏览器中打开 AppletPage.html
以查看 Math 小程序。
Note: If you don’t see the applet running, you need to install at least the Java SE Development Kit (JDK) 6 update 10 release.
Note: If you don’t see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.
检查放置在 JavaScript 代码调用的 applet 上的安全限制。
下载源代码为调用 Applet 方法从 JavaScript 代码示例进一步试验。