呼叫通过JavaScript Java小程序的方法 [英] Call a java applet method via javascript

查看:260
本文介绍了呼叫通过JavaScript Java小程序的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能调用一个函数,当一个特定的事件在我的Java小程序出现?

How can I call a function when a specific event occurs in my java Applet ?

在我的javascript我有以下code,总是返回空值。

In my Javascript I has the following code, that always return a empty value.

$(function () {
    alert(document.applets[0].returnClientId());
});

我需要调用此警报,只是当执行一个特定的方法。更具体地,称为识别方法,存在于我的Java小程序

I need to call this alert, just when a specific method is executed. To be more specific, the method called identify, that exists in my java Applet.

的识别方法,存在于我的Util.class,即不要从JApplet的延伸。
我FormMain.class从JApplet的延伸,我称之为从这个Util.class一些方法(包括识别方法)。

The identify method, exists in my Util.class, that don't extends from JApplet. My FormMain.class extends from JApplet and I call some methods (including the identify method) from this Util.class.

更新:我的Java code

UPDATE: My Java Code

public String getClientid() {
    return clientid;
}

public void setClientid(String clientid) {
    this.clientid = clientid;
}

public String returnClientId() {
    return getClientid();
}

public void identify() {
    try {
        fingerprintSDK.prepareForIdentification(this.template);

        ResultSet rs = identifyStmt.executeQuery();

        while (rs.next()) {         
            byte[] templateBuffer = rs.getBytes("template");

            Template referenceTemplate = new Template(templateBuffer);

            boolean matched = fingerprintSDK.identify(referenceTemplate);

            if (matched) {
            // ui is my FormMain instance
                ui.showImage(GrFingerJava.getBiometricImage(template,
                        fingerprint, fingerprintSDK));

                ui.writeLog("Found. Client = "
                        + rs.getString("Name"));

                ui.setClienteid(rs.getString("Cliente_Id"));

                ui.disableTemplate();                   
                return;
            }
        }

        ui.writeLog("Not Found.");
        ui.enableTemplate();
    } catch (SQLException e) {

        ui.writeLog(e.getMessage());
    } catch (GrFingerJavaException e) {
        ui.writeLog(e.getMessage());
    }
}

在我的用户把手指在生物识别设备是刚刚执行的鉴定方法。

The Identify method is executed just when my User put the finger in the biometric device.

有人有一些想法?

推荐答案

假设你有以下的JS函数

Suppose you have the following JS function

function foo(client) {
    alert(client);
}

您会修改 Utils.java 如下:

public void identify() {
    // the rest of your code
    String id = rs.getString("Cliente_Id");
    ui.setClienteid(id);
    // call the Javascript function
    JSObject.getWindow(ui).eval(String.format("foo(%s)", id));
}


  • 从applet的Javascript调用code

  • Java来的Javascript通信(Oracle官方文档)

    • Invoking Javascript code from an Applet
    • Java to Javascript communication (Official Oracle docs)
    • 要使用编译code netscape.javascript。* 包您需要的 $ JAVA_HOME / JRE / lib目录/ plugin.jar 看到这里

      To compile the code using netscape.javascript.* package you need the $JAVA_HOME/jre/lib/plugin.jar. See here

      为了调用JavaScript,Java的code使用
        netscape.javascript.JSObject和netscape.javascript.JSException
        类。由于Java 2的发布,标准版1.4版,
        在JAR文件中的jre / lib中提供了这些类/ plugin.jar内
        无论是Java开发工具和Java运行时环境。如果你
        引用这些JavaScript类,您将需要plugin.jar增加
        你的编译类路径。这可以通过你的Java IDE来完成,如果
        您可以使用一个,或者通过-classpath命令行参数
        Java编译器javac的。

      In order to call JavaScript, Java code uses the netscape.javascript.JSObject and netscape.javascript.JSException classes. Since the release of Java 2, Standard Edition version 1.4, these classes are provided in the jar file jre/lib/plugin.jar within either the Java Development Kit or Java Runtime Environment. If you reference these JavaScript classes, you will need to add plugin.jar to your compilation classpath. This can be done through your Java IDE, if you use one, or by passing the -classpath command-line argument to the Java compiler javac.

      在运行时,Java插件会自动使这些类
        适用于小程序,所以没有给小程序改变或它是如何设置
        是必要的。

      At run-time, the Java Plug-In automatically makes these classes available to applets, so no changes to the applet or how it is set up are necessary.

      这篇关于呼叫通过JavaScript Java小程序的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆