从 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辅助类的引用。

  1. package jstojava;
  2. import java.applet.Applet;
  3. public class MathApplet extends Applet{
  4. public String userName = null;
  5. public String getGreeting() {
  6. return "Hello " + userName;
  7. }
  8. public Calculator getCalculator() {
  9. return new Calculator();
  10. }
  11. public DateHelper getDateHelper() {
  12. return new DateHelper();
  13. }
  14. public void printOut(String text) {
  15. System.out.println(text);
  16. }
  17. }

Calculator 类中的方法允许用户设置两个值,添加数字,并检索范围内的数字。

  1. package jstojava;
  2. public class Calculator {
  3. private int a = 0;
  4. private int b = 0; // assume b > a
  5. public void setNums(int numA, int numB) {
  6. a = numA;
  7. b = numB;
  8. }
  9. public int add() {
  10. return a + b;
  11. }
  12. public int [] getNumInRange() {
  13. int x = a;
  14. int len = (b - a) + 1;
  15. int [] range = new int [len];
  16. for (int i = 0; i < len; i++) {
  17. range[i]= x++;
  18. System.out.println("i: " + i + " ; range[i]: " + range[i]);
  19. }
  20. return range;
  21. }
  22. }

DateHelper 类的getDate方法返回当前日期。

  1. package jstojava;
  2. import java.util.Date;
  3. import java.text.SimpleDateFormat;
  4. public class DateHelper {
  5. public static String label = null;
  6. public String getDate() {
  7. return label + " " + new SimpleDateFormat().format(new Date());
  8. }
  9. }

部署 Applet

在网页中部署 applet, AppletPage.html部署 applet 时,请确保为 applet 指定 id。稍后使用 applet id 来获取对 applet 对象的引用。

  1. <script src=
  2. "https://www.java.com/js/deployJava.js"></script>
  3. <script>
  4. <!-- applet id can be used to get a reference to
  5. the applet object -->
  6. var attributes = { id:'mathApplet',
  7. code:'jstojava.MathApplet', width:1, height:1} ;
  8. var parameters = { jnlp_href: 'math_applet.jnlp'} ;
  9. deployJava.runApplet(attributes, parameters, '1.6');
  10. </script>

接下来,将一些 JavaScript 代码添加到 AppletPage.html网页。 JavaScript 代码可以使用 applet id 作为 applet 对象的引用,并调用 applet 的方法。在下面显示的示例中,JavaScript 代码设置 applet 的公共成员变量,调用公共方法,并检索对 applet 引用的另一个对象的引用(Calculator)。 JavaScript 代码能够处理原始,数组和对象返回类型。

  1. <script language="javascript">
  2. function enterNums(){
  3. var numA = prompt('Enter number \'a\'?','0');
  4. var numB = prompt(
  5. 'Enter number \'b\' (should be greater than number \'a\' ?','1');
  6. // set applet's public variable
  7. mathApplet.userName = "John Doe";
  8. // invoke public applet method
  9. var greeting = mathApplet.getGreeting();
  10. // get another class referenced by applet and
  11. // invoke its methods
  12. var calculator = mathApplet.getCalculator();
  13. calculator.setNums(numA, numB);
  14. // primitive datatype returned by applet
  15. var sum = calculator.add();
  16. // array returned by applet
  17. var numRange = calculator.getNumInRange();
  18. // check Java console log for this message
  19. mathApplet.printOut("Testing printing to System.out");
  20. // get another class, set static field and invoke its methods
  21. var dateHelper = mathApplet.getDateHelper();
  22. dateHelper.label = "Today\'s date is: ";
  23. var dateStr = dateHelper.getDate();
  24. <!-- ... -->
  25. </script>

当数字 a = 0 且 b = 5 时,Math 小程序在网页上显示以下结果:

  1. Results of JavaScript to Java Communication
  2. Hello John Doe
  3. a = 0 ; b = 5
  4. Sum: 5
  5. Numbers in range array: [ 0, 1, 2, 3, 4, 5 ]
  6. 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 代码示例进一步试验。