문제

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>
도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top