import java.net.*;
import java.io.*;
import java.applet.*;
public class GetClientIP extends Applet {
public void init() {
try {
InetAddress thisIp =
InetAddress.getLocalHost();
System.out.println("IP:"+thisIp.getHostAddress());
}
catch(Exception e) {
e.printStackTrace();
}
}
}<HTML><HEAD></HEAD><BODY>
<APPLET CODE="GetClientIP.class"
HEIGHT=10 WIDTH=10>
</APPLET>
Check JAVA console for output
</BODY></HTML>NOTE: Netscape returns the IP address with the default security settings so it's not problem. With IE5, you must go to the security TAB, Internet, Java Custom, Edit and select "Allow access to all IP address".
Using a Servlet, this can be done with :
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException {
String IP = req.getRemoteAddr();
}
Written and compiled by Réal Gagnon ©1998-2005
[ home ]