Thursday, August 12, 2010

Get mac address ip address in jsp java

Get MAC address (Physical Address) and IP Address of client machine in jsp java

How to get MAC address (Physical Address) and IP Address of client machine.
listed below code that is show client machine's MAC address
(Physical Address) and IP Address.

use listed below code and you will find something like that:

100.202.202.201
11-26-7Z-50-89-9B

 The "100.202.202.201" is the IP Address and "11-26-7Z-50-89-9B" is the
Mac Address of the client machine.


import these packages:

<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>


<%
InetAddress inetAddress;
StringBuilder sb = new StringBuilder();
String ipAddress="",macAddress="";
int i=0;
try {
    inetAddress=InetAddress.getLocalHost();
    ipAddress=inetAddress.getHostAddress();
    NetworkInterface network=NetworkInterface.getByInetAddress(inetAddress);
     byte[] hw=network.getHardwareAddress();
    for(i=0; i<hw.length; i++)
        sb.append(String.format("%02X%s", hw[i], (i < hw.length - 1) ? "-" : ""));   
    macAddress=sb.toString();
} catch(Exception e) {
    out.print("<br/>"+e.toString());
    macAddress="-";
}
out.print("<br/>"+ipAddress);
out.print("<br/>"+macAddress);
%>

2 comments:

  1. This is logging the J2EE server's MAC and not the client machines mac when implemented in a JSP

    ReplyDelete
  2. Thank you , the code worked for me.

    ReplyDelete