Sometimes you need to pass parameters from an applet to an HTML file. For instance, you may want to detect, using an applet, the IP address of the viewer when they are trying to access your page and send them to a HTML page that will dynamically use it.
applet_test.java: This is the sample Java applet that returns the hostname of the machine and the IP address associated with that hostname.
--- applet_test.java ---
import java.applet.Applet;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class applet_test extends Applet {
private InetAddress addr = null;
public void init() {
try {
addr = InetAddress.getLocalHost();
}
catch (UnknownHostException e) {
System.exit(0);
}
}
public InetAddress getLHost() {
return addr;
}
}
test.html: The sample HTML file
test.html calls a Java applet (
applet_test.class) and gets the
getLHost() parameter from this applet:
--- test.html ---
<html>
<body>
<APPLET CODE=applet_test.class Width=0 Height=0></APPLET>
<script type="text/javascript">
<!--
document.write(document.applets[0].getLHost());
//-->
</script>
</body>
</html>
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible.
Submit your tip here.