Question

I'm trying to create an excel file and save to local file system using java applet. After sign the application, i can successfully create the file by directly invoking the applet. However, when i try to call the method from javascript, it failed without any error message. I'm wondering if there is any policy control to prevent java method to be called from javascript?

 <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>
Was it helpful?

Solution

Methods in a trusted applet that are invoked using JavaScript need to be wrapped in a PrivilegedAction and called using one of the AccessController.doPrivileged(..) variants.

Sandboxed file-system access

Plug-In 2 JREs (Oracle's 1.6.0_10+ JRE for example) offer sandboxed access to the local file-system using the JNLP API file services (specifically the FileSaveService). See the file service demo. for an example.

Applet element

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.

OTHER TIPS

There may be several issues here.

1) You have to use the JavaScript deployment method for the applet (see link).

2) If the method you try to invoke is not in the applet-child itself, you first have to get an instance of the class in question and then invoke it's method, e.g. (for Calculator class):

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

Moor info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top