Javascript返回“错误:错误调用NPObject上的方法!”当javascript调用Applet公共方法时

时间:2016-05-25 20:38:16

标签: javascript java html applet

我正在尝试返回一些客户信息,但是当我在我的机器上运行applet时,浏览器会返回这些错误:

火狐: 错误:在NPObject上调用方法时出错!

铬: encript.js:7未捕获的TypeError:applet.returnMD5不是函数

Main.java

public class Main extends Applet {

private String ip;
private String macLocal;
private String md5;
private InetAddress ipLocal;

@Override
public void init() {
    super.init(); //To change body of generated methods, choose Tools | Templates.
}



public String returnIp(){
    try {
        ipLocal = InetAddress.getLocalHost();
        ip = ipLocal.getHostAddress();
    } catch (UnknownHostException ex) {
        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ip;
}
public String returnMac(){
    try {
        ipLocal = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ipLocal);
        byte[] mac = network.getHardwareAddress();
        StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
    }
            macLocal = sb.toString();
    } catch (SocketException ex) {
        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnknownHostException ex) {
        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
    }
    return macLocal;
}
public String returnMD5(){
    try {
        MessageDigest m=MessageDigest.getInstance("MD5");
        String ipMaisMac = new StringBuffer(retornaIp()).reverse().toString() + new StringBuffer(retornaMac()).reverse().toString() ;
        m.update(ipMaisMac.getBytes(),0,ipMaisMac.length());
        BigInteger i = new BigInteger(1, m.digest()); 
        md5 = String.format("%1$032X", i); 
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
    }
    return md5;
}

}

encript.js

function encriptar() {
    var applet = document.getElementById("principal-object")
    alert(typeof(applet));
    if(typeof(applet) == "undefined") {
        alert("Undefined")
    } else {
        document.getElementById("token").value = applet.returnMD5();
        document.getElementById("local_ip").value = applet.returnIp();
        document.getElementById("tolocal_macken").value = applet.returnMac();
    }
}

的index.html

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src="encript.js"></script>
</head>

<body onload="encript()">
    <form action="login.php">
        Login:
        <input type="text" name="login" value="" /> Password:
        <input type="password" name="password" value="" />
        <input type="hidden" name="local_ip" id="local_ip" value="00" />
        <input type="hidden" name="local_mac" id="local_mac" value="00" />
        <input type="hidden" name="token" id="token" value="00" />
        <button>Login!</button>
    </form>
    <object id="principal-object" width="256" height="256">
        <param name="archive" value="Encript.jar" />
        <param name="code" value="br.com.encript.applets.Main" />
        <param name="mayscript" value="true" />
    </object>
</body>

</html>

0 个答案:

没有答案