1

我正在尝试使用 java 小程序创建一个 excel 文件并保存到本地文件系统。签署应用程序后,我可以通过直接调用小程序成功创建文件。但是,当我尝试从 javascript 调用该方法时,它失败且没有任何错误消息。我想知道是否有任何策略控制来防止从 javascript 调用 java 方法?

 <html>
<script language="JavaScript">
function createExcel()
{
    document.excel.createExcel();

    document.excel.setMessage("hello world from js");
}
</script>
<body>
<input type="button" value="Generate Excel" onclick="createExcel()" />
<applet id='applet' name='excel' archive='Office.jar,poi-3.7-20101029.jar' code='com.broadridge.Office.class' width='100' height='100'></applet>
</body>
</html>
4

2 回答 2

4

使用 JavaScript 调用的受信任小程序中的方法需要包装在 a 中PrivilegedAction并使用其中一种AccessController.doPrivileged(..)变体调用。

沙盒文件系统访问

插件 2 JRE(例如 Oracle 的 1.6.0_10+ JRE)使用JNLP API文件服务(特别是FileSaveService. 请参阅文件服务演示。例如。

小程序元素

An applet called by JS should declare that it is scriptable in the HTML. As for getting that 'HTML', the best way to write it is using Oracle's deployJava.js. It will not only write the applet element the best way that is currently known for each browser, but does JRE presence (is Java installed?) and minimum version (is the JRE the minimum version needed to run this applet?) checking.

于 2011-10-20T21:49:57.770 回答
1

这里可能有几个问题。

1)您必须使用小程序的 JavaScript 部署方法(见链接)。

2)如果您尝试调用的方法不在 applet-child 本身中,您首先必须获取相关类的实例,然后调用它的方法,例如(对于 Calculator 类):

var calculator = mathApplet.getCalculator();
calculator.setNums(numA, numB);

摩尔信息

于 2011-10-20T06:41:40.333 回答