{"id":13339,"date":"2024-03-15T06:52:03","date_gmt":"2024-03-15T06:52:03","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/"},"modified":"2025-08-05T16:45:45","modified_gmt":"2025-08-05T16:45:45","slug":"three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/","title":{"rendered":"Java Get Client MAC Address"},"content":{"rendered":"<p>In Java\/JSP, there are three methods available for obtaining the MAC address of a client&#8217;s network card.<\/p>\n<p>One common way to retrieve a MAC address is by using the NetworkInterface class in Java. This can be done by calling the getHardwareAddress() method of the NetworkInterface class. Here is an example code snippet:<\/p>\n<pre class=\"post-pre\"><code class=\"lang-java\">import java.net.NetworkInterface;\r\nimport java.net.SocketException;\r\n\r\npublic class GetMacAddress {\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/ \u83b7\u53d6\u672c\u5730\u7f51\u7edc\u63a5\u53e3\u5bf9\u8c61\r\n            NetworkInterface networkInterface = NetworkInterface.getByName(\"eth0\");\r\n\r\n            \/\/ \u83b7\u53d6MAC\u5730\u5740\r\n            byte[] mac = networkInterface.getHardwareAddress();\r\n\r\n            \/\/ \u5c06MAC\u5730\u5740\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\r\n            StringBuilder macAddress = new StringBuilder();\r\n            for (int i = 0; i &lt; mac.length; i++) {\r\n                macAddress.append(String.format(\"%02X%s\", mac[i], (i &lt; mac.length - 1) ? \"-\" : \"\"));\r\n            }\r\n\r\n            System.out.println(\"MAC\u5730\u5740\uff1a\" + macAddress.toString());\r\n        } catch (SocketException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>To obtain the MAC address, you can utilize the InetAddress class in Java. You can start by calling the getLocalHost() method to get the InetAddress object of the local host. Then, you can use the getHostName() method to retrieve the host name, followed by calling the getByName() method to get the InetAddress object. Lastly, you can use the getHardwareAddress() method to retrieve the MAC address. Here is an example code:<\/p>\n<pre class=\"post-pre\"><code class=\"lang-java\">import java.net.InetAddress;\r\nimport java.net.NetworkInterface;\r\nimport java.net.SocketException;\r\n\r\npublic class GetMacAddress {\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/ \u83b7\u53d6\u672c\u5730\u4e3b\u673aInetAddress\u5bf9\u8c61\r\n            InetAddress localHost = InetAddress.getLocalHost();\r\n\r\n            \/\/ \u83b7\u53d6\u4e3b\u673a\u540d\r\n            String hostname = localHost.getHostName();\r\n\r\n            \/\/ \u83b7\u53d6\u672c\u5730\u7f51\u7edc\u63a5\u53e3\u5bf9\u8c61\r\n            NetworkInterface networkInterface = NetworkInterface.getByName(hostname);\r\n\r\n            \/\/ \u83b7\u53d6MAC\u5730\u5740\r\n            byte[] mac = networkInterface.getHardwareAddress();\r\n\r\n            \/\/ \u5c06MAC\u5730\u5740\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u4e32\r\n            StringBuilder macAddress = new StringBuilder();\r\n            for (int i = 0; i &lt; mac.length; i++) {\r\n                macAddress.append(String.format(\"%02X%s\", mac[i], (i &lt; mac.length - 1) ? \"-\" : \"\"));\r\n            }\r\n\r\n            System.out.println(\"MAC\u5730\u5740\uff1a\" + macAddress.toString());\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>3. Utilizing JavaScript and JSP: JavaScript can be used to retrieve the MAC address on the client side, and then pass it to a JSP page through Ajax. Here is an example code:<\/p>\n<pre class=\"post-pre\"><code class=\"lang-html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Get MAC Address&lt;\/title&gt;\r\n    &lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\"&gt;&lt;\/script&gt;\r\n    &lt;script&gt;\r\n        function getMacAddress() {\r\n            var macAddress = null;\r\n            new ActiveXObject(\"WScript.Shell\").Run(\"cmd \/k getmac \/v\", 0, true);\r\n            var wmi = new ActiveXObject(\"WbemScripting.SWbemLocator\");\r\n            var service = wmi.ConnectServer(\".\");\r\n            var properties = service.ExecQuery(\"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True\");\r\n            var e = new Enumerator(properties);\r\n            for (; !e.atEnd(); e.moveNext()) {\r\n                var p = e.item();\r\n                macAddress = p.MACAddress;\r\n                break;\r\n            }\r\n            return macAddress;\r\n        }\r\n\r\n        $(document).ready(function() {\r\n            var macAddress = getMacAddress();\r\n            $.ajax({\r\n                url: \"getMacAddress.jsp\",\r\n                type: \"POST\",\r\n                data: {macAddress: macAddress},\r\n                success: function(response) {\r\n                    console.log(response);\r\n                }\r\n            });\r\n        });\r\n    &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p>In getMacAddress.jsp, you can use request.getParameter(&#8220;macAddress&#8221;) to retrieve the MAC address parameter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java\/JSP, there are three methods available for obtaining the MAC address of a client&#8217;s network card. One common way to retrieve a MAC address is by using the NetworkInterface class in Java. This can be done by calling the getHardwareAddress() method of the NetworkInterface class. Here is an example code snippet: import java.net.NetworkInterface; import [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[17739,17742,17743,17740,17741],"class_list":["post-13339","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java-mac","tag-java-socket","tag-jsp-client","tag-jsp-network","tag-mac-address"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Get Client MAC Address - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Get Client MAC Address\" \/>\n<meta property=\"og:description\" content=\"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T06:52:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T16:45:45+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"Java Get Client MAC Address\",\"datePublished\":\"2024-03-15T06:52:03+00:00\",\"dateModified\":\"2025-08-05T16:45:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\"},\"wordCount\":176,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Java MAC\",\"Java socket\",\"JSP client\",\"JSP network\",\"MAC address\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\",\"name\":\"Java Get Client MAC Address - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T06:52:03+00:00\",\"dateModified\":\"2025-08-05T16:45:45+00:00\",\"description\":\"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Get Client MAC Address\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Get Client MAC Address - Blog - Silicon Cloud","description":"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/","og_locale":"en_US","og_type":"article","og_title":"Java Get Client MAC Address","og_description":"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples","og_url":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T06:52:03+00:00","article_modified_time":"2025-08-05T16:45:45+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"Java Get Client MAC Address","datePublished":"2024-03-15T06:52:03+00:00","dateModified":"2025-08-05T16:45:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/"},"wordCount":176,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Java MAC","Java socket","JSP client","JSP network","MAC address"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/","url":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/","name":"Java Get Client MAC Address - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T06:52:03+00:00","dateModified":"2025-08-05T16:45:45+00:00","description":"Learn 3 methods to retrieve client network card MAC address using Java\/JSP with code examples","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/three-ways-to-obtain-the-clients-network-card-mac-address-using-java-jsp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java Get Client MAC Address"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=13339"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13339\/revisions"}],"predecessor-version":[{"id":157303,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13339\/revisions\/157303"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=13339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=13339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=13339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}