当前位置: 动力学知识库 > 问答 > 编程问答 >

java - return statement inside applet method which is called from javascript

问题描述:

my scenario is like i invoke applet API from javascript whose return statement is boolean, but i unable to get boolean response from that method.

let me tell what happening inside that method

  1. from js control is going to applet method abc()

  2. inside abc() method i am invoking servlet which is does some process at server end and returns response which i am getting with out any issues.

  3. after this i am doing some regular java stuff and trying to return true or false

  4. I am not getting this response in javascript

Note: This points may be helpful to figure out the issue

1.My applet implements runnable interface which is used for show confirmation dialog to use developed using swing components, this code triggers in some scenarios not every.

  1. I use servlets which returns json response

    Let me know if you need any details

  2. I am using below function in foreach

    function abc()

    {

    var status = printerApplet.startBulkPersoProcessing(a, b, c);

    alert("Is it coming here:::");

    alert(status);

    }

This status is not printed what ever we do.

网友答案:

first you should make sure you declared your applet like the following :

<APPLET code="MyApplet.class" height="0" id="myApplet" name="myApplet"
                                    archive="/path/to/MyApplet.jar"></APPLET>

after that to call MyApplet from JS like the following :

function callMyAppletFromJS() {
            alert("callMyAppletFromJS");
                document.getElementById('myApplet').jsAppletCall();
      }

in your applet call the JS back like the following :

public void jsAppletCall() {
      //       do you java stuff here 
      // then 
      // call the javascript from the applet using the JSOBject 
              JSObject win = JSObject.getWindow(this);
              win.eval("callJSFromMyAppletMessage();");
     }

and the JS function to be called from the applet like the following :

function callJSFromMyAppletMessage() {
     alert("callJSFromMyAppletMessage");
      }

so the way to call the JS from applet is using the JSObject , please check the docs here for the JSObject as it describes the use for this class .

also as @Hemant mentioned , please check the oracle tutorial here about this subject .

Note : but please be aware if you are calling your applet from JS , even if its signed applet , it will be treated as unsigned applet , check the oracle docs here .

Hope that Helps .

网友答案:

You can use Java's JSObject to callback a JS function defined in your HTML view.

This is the one of the convenient way of handling async function calls.

More info on JSObject implementation...

http://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html

Hope it helps!

分享给朋友:
您可能感兴趣的文章:
随机阅读: