我正在尝试制作一个JAVA Applet来检索用户的MAC地址,将其转换为MD5并将其发送到javascript函数,以便它可以完成任务。
我打算将此MD5附加到其他一些输入数据的表单中。
到目前为止,我可以检索MAC地址并成功将其转换为MD5。我不能做的是将它传递给我的javascript函数。
这是我的HTML代码:
<body>
<script type="text/javascript">
function dohash(hash) { alert(hash); }
</script>
<APPLET code="start.class" width="200" height="200"></APPLET>
</body>
</html>
这是JAVA代码:
import netscape.javascript.JSObject;
import java.applet.*;
import java.security.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.math.BigInteger;
public class start extends Applet {
private static final long serialVersionUID = 1L;
JSObject win;
public void main() {
win = (JSObject)JSObject.getWindow(this);
}
public void init() {
InetAddress ip;
String hashtext = "a";
try {
ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
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) ? "-" : ""));
}
try {
String plaintext = sb.toString();
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(plaintext.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1,digest);
hashtext = bigInt.toString(16);
while(hashtext.length() < 32 ){
hashtext = "0"+hashtext;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
win.eval("alert('123');");
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
}
}
这是Eclipse控制台上的响应:
java.lang.NullPointerException
at start.init(start.java:50)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:744)
答案 0 :(得分:5)
main
。在win
方法
init
引用
public void init() {
win = (JSObject)JSObject.getWindow(this);
...
}