jsp和java代码如下:JAVA:
import java.applet.Applet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
public class MACAddressApplet extends Applet
{
private static final long serialVersionUID = 6866002748735327341L;
public static String sep = ":";
public static String format = "%02X";
/**
* getMacAddress - return the first mac address found
* separator - byte seperator default ":"
* format - byte formatter default "%02X"
*
* @param ni - the network interface
* @return String - the mac address as a string
* @throws SocketException - pass it on
*/
public static String macToString( NetworkInterface ni ) throws SocketException
{
return macToString( ni, MACAddressApplet.sep, MACAddressApplet.format );
}
/**
* getMacAddress - return the first mac address found
*
* @param ni - the network interface
* @param separator - byte seperator default ":"
* @param format - byte formatter default "%02X"
* @return String - the mac address as a string
* @throws SocketException - pass it on
*/
public static String macToString( NetworkInterface ni, String separator, String format ) throws SocketException
{
byte mac [] = ni.getHardwareAddress();
if( mac != null ) {
StringBuffer macAddress = new StringBuffer( "" );
String sep = "";
for( byte o : mac ) {
macAddress.append( sep ).append( String.format( format, o ) );
sep = separator;
}
return macAddress.toString();
}
return null;
}
/**
* getMacAddress - return the first mac address found
*
* @return the mac address or undefined
*/
public static String getMacAddress()
{
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
// not all interface will have a mace address for instance loopback on windows
while( nis.hasMoreElements() ) {
String mac = macToString( nis.nextElement() );
if( mac != null )
return mac;
}
} catch( SocketException ex ) {
System.err.println( "SocketException:: " + ex.getMessage() );
ex.printStackTrace();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return "undefined";
}
/**
* getMacAddressesJSON - return all mac addresses found
*
* @return a JSON array of strings (as a string)
*/
public static String getMacAddressesJSON()
{
try {
String macs [] = getMacAddresses();
String sep = "";
StringBuffer macArray = new StringBuffer( "['" );
for( String mac: macs ) {
macArray.append( sep ).append( mac );
sep = "','";
}
macArray.append( "']" );
return macArray.toString();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return "[]";
}
/**
* getMacAddresses - return all mac addresses found
*
* @return array of strings (mac addresses) empty if none found
*/
public static String [] getMacAddresses()
{
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
ArrayList<String> macs = new ArrayList<String>();
while( nis.hasMoreElements() ) {
String mac = macToString( nis.nextElement() );
// not all interface will have a mac address for instance loopback on windows
if( mac != null ) {
macs.add( mac );
}
}
return macs.toArray( new String[0] );
} catch( SocketException ex ) {
System.err.println( "SocketException:: " + ex.getMessage() );
ex.printStackTrace();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return new String[0];
}
/**
* getMacAddresses - return all mac addresses found
*
* @param sep - use a different separator
*/
public static void setSep( String sep )
{
try {
MACAddressApplet.sep = sep;
} catch( Exception ex ) {
// don't care
}
}
/**
* getMacAddresses - return all mac addresses found
*
* @param format - the output format string for bytes that can be overridden default hex.
*/
public static void setFormat( String format )
{
try {
MACAddressApplet.format = format;
} catch( Exception ex ) {
// don't care
}
}
public static String helloword()
{
return "Hello World!";
}
public static void main( String... args )
{
System.err.println( " MacAddress = " + getMacAddress() );
setSep( "-" );
String macs [] = getMacAddresses();
for( String mac : macs )
System.err.println( " MacAddresses = " + mac );
setSep( ":" );
System.err.println(getMacAddress());
}
}
JSP:
<html>
<head></head>
<body>
<a href="" onclick="getMacAddress();"> Get "first" MAC Address</a>
<br/>
<br/>
<a href="" onclick="getMacAddressesJSON();"> Get all MAC Addresses</a>
<!--[if !IE]> Firefox and others will use outer object -->
<!-- <object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "<%=request.getContextPath()%>/rabbit/jsp/applet/jre/jre-6u22-windows-i586.exe"
WIDTH = "100%" HEIGHT = "20" name="MACApplet">
<param name="name" value="MACApplet">
<param name="codebase" value="applet">
<param name ="code" value="MACAddressApplet.class" >
<param name="archive" value="MACAddressApplet.jar">
<param name = "type" value = "application/x-java-applet;jpi-version=1.6.0.22">
<param name = "scriptable" value = "true">
<comment>
<embed type = "application/x-java-applet;jpi-version=1.6.0.22"
CODE = "MACAddressApplet.class"
WIDTH = "500" HEIGHT = "400"
JAVA_CODEBASE = "applet"
scriptable = "true"
pluginspage ="rabbit/jsp/applet/jre/jre-6u22-windows-i586.exe">
<noembed> </noembed>
</embed>
</comment>
</object>-->
<!--<![endif]-->
<applet
name="MACApplet" codebase= "<%=request.getContextPath()%>/rabbit/jsp/applet"
width="100%" code = "MACAddressApplet.class"
height="20" archive="MACAddressApplet.jar" mayscript="mayscript"></applet>
<script type="text/javascript">
function getMacAddress()
{
//document.applets["MACApplet"].setSep( "-" );
//alert( "Mac Address = " + document.applets["MACApplet"].getMacAddresses() );
document.MACApplet.setSep( ":" );
document.applets["MACApplet"].setFormat( "%02x" );
//alert(document.applets["MACApplet"].helloword());
alert(document.applets["MACApplet"].getMacAddress());
}
function getMacAddressesJSON()
{
document.MACApplet.setSep( ":" );
document.applets["MACApplet"].setFormat( "%02x" );
var macs = eval( String( document.applets["MACApplet"].getMacAddressesJSON() ) );
var mac_string = "";
for( var idx = 0; idx < macs.length; idx ++ )
mac_string += "\t" + macs[ idx ] + "\n "; alert( "Mac Addresses = \n" + mac_string );
}
</script>
</body>
</html>
import java.applet.Applet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
public class MACAddressApplet extends Applet
{
private static final long serialVersionUID = 6866002748735327341L;
public static String sep = ":";
public static String format = "%02X";
/**
* getMacAddress - return the first mac address found
* separator - byte seperator default ":"
* format - byte formatter default "%02X"
*
* @param ni - the network interface
* @return String - the mac address as a string
* @throws SocketException - pass it on
*/
public static String macToString( NetworkInterface ni ) throws SocketException
{
return macToString( ni, MACAddressApplet.sep, MACAddressApplet.format );
}
/**
* getMacAddress - return the first mac address found
*
* @param ni - the network interface
* @param separator - byte seperator default ":"
* @param format - byte formatter default "%02X"
* @return String - the mac address as a string
* @throws SocketException - pass it on
*/
public static String macToString( NetworkInterface ni, String separator, String format ) throws SocketException
{
byte mac [] = ni.getHardwareAddress();
if( mac != null ) {
StringBuffer macAddress = new StringBuffer( "" );
String sep = "";
for( byte o : mac ) {
macAddress.append( sep ).append( String.format( format, o ) );
sep = separator;
}
return macAddress.toString();
}
return null;
}
/**
* getMacAddress - return the first mac address found
*
* @return the mac address or undefined
*/
public static String getMacAddress()
{
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
// not all interface will have a mace address for instance loopback on windows
while( nis.hasMoreElements() ) {
String mac = macToString( nis.nextElement() );
if( mac != null )
return mac;
}
} catch( SocketException ex ) {
System.err.println( "SocketException:: " + ex.getMessage() );
ex.printStackTrace();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return "undefined";
}
/**
* getMacAddressesJSON - return all mac addresses found
*
* @return a JSON array of strings (as a string)
*/
public static String getMacAddressesJSON()
{
try {
String macs [] = getMacAddresses();
String sep = "";
StringBuffer macArray = new StringBuffer( "['" );
for( String mac: macs ) {
macArray.append( sep ).append( mac );
sep = "','";
}
macArray.append( "']" );
return macArray.toString();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return "[]";
}
/**
* getMacAddresses - return all mac addresses found
*
* @return array of strings (mac addresses) empty if none found
*/
public static String [] getMacAddresses()
{
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
ArrayList<String> macs = new ArrayList<String>();
while( nis.hasMoreElements() ) {
String mac = macToString( nis.nextElement() );
// not all interface will have a mac address for instance loopback on windows
if( mac != null ) {
macs.add( mac );
}
}
return macs.toArray( new String[0] );
} catch( SocketException ex ) {
System.err.println( "SocketException:: " + ex.getMessage() );
ex.printStackTrace();
} catch( Exception ex ) {
System.err.println( "Exception:: " + ex.getMessage() );
ex.printStackTrace();
}
return new String[0];
}
/**
* getMacAddresses - return all mac addresses found
*
* @param sep - use a different separator
*/
public static void setSep( String sep )
{
try {
MACAddressApplet.sep = sep;
} catch( Exception ex ) {
// don't care
}
}
/**
* getMacAddresses - return all mac addresses found
*
* @param format - the output format string for bytes that can be overridden default hex.
*/
public static void setFormat( String format )
{
try {
MACAddressApplet.format = format;
} catch( Exception ex ) {
// don't care
}
}
public static String helloword()
{
return "Hello World!";
}
public static void main( String... args )
{
System.err.println( " MacAddress = " + getMacAddress() );
setSep( "-" );
String macs [] = getMacAddresses();
for( String mac : macs )
System.err.println( " MacAddresses = " + mac );
setSep( ":" );
System.err.println(getMacAddress());
}
}
JSP:
<html>
<head></head>
<body>
<a href="" onclick="getMacAddress();"> Get "first" MAC Address</a>
<br/>
<br/>
<a href="" onclick="getMacAddressesJSON();"> Get all MAC Addresses</a>
<!--[if !IE]> Firefox and others will use outer object -->
<!-- <object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "<%=request.getContextPath()%>/rabbit/jsp/applet/jre/jre-6u22-windows-i586.exe"
WIDTH = "100%" HEIGHT = "20" name="MACApplet">
<param name="name" value="MACApplet">
<param name="codebase" value="applet">
<param name ="code" value="MACAddressApplet.class" >
<param name="archive" value="MACAddressApplet.jar">
<param name = "type" value = "application/x-java-applet;jpi-version=1.6.0.22">
<param name = "scriptable" value = "true">
<comment>
<embed type = "application/x-java-applet;jpi-version=1.6.0.22"
CODE = "MACAddressApplet.class"
WIDTH = "500" HEIGHT = "400"
JAVA_CODEBASE = "applet"
scriptable = "true"
pluginspage ="rabbit/jsp/applet/jre/jre-6u22-windows-i586.exe">
<noembed> </noembed>
</embed>
</comment>
</object>-->
<!--<![endif]-->
<applet
name="MACApplet" codebase= "<%=request.getContextPath()%>/rabbit/jsp/applet"
width="100%" code = "MACAddressApplet.class"
height="20" archive="MACAddressApplet.jar" mayscript="mayscript"></applet>
<script type="text/javascript">
function getMacAddress()
{
//document.applets["MACApplet"].setSep( "-" );
//alert( "Mac Address = " + document.applets["MACApplet"].getMacAddresses() );
document.MACApplet.setSep( ":" );
document.applets["MACApplet"].setFormat( "%02x" );
//alert(document.applets["MACApplet"].helloword());
alert(document.applets["MACApplet"].getMacAddress());
}
function getMacAddressesJSON()
{
document.MACApplet.setSep( ":" );
document.applets["MACApplet"].setFormat( "%02x" );
var macs = eval( String( document.applets["MACApplet"].getMacAddressesJSON() ) );
var mac_string = "";
for( var idx = 0; idx < macs.length; idx ++ )
mac_string += "\t" + macs[ idx ] + "\n "; alert( "Mac Addresses = \n" + mac_string );
}
</script>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货