<%@ page import="java.util.Enumeration" %>
<%@ page import="java.net.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
    String ip1 = GetServerIP();
    out.print("访问的IP是:"+ip1.substring(ip1.lastIndexOf("."))+"</br>");
    String ip2 = GetServerIP2();
    out.print("访问的IP是:"+ip2.substring(ip2.lastIndexOf("."))+"</br>");
%>

<%!

    public String GetServerIP() {
        String hostIp = "" ;
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            hostIp = localhost.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return hostIp;
    }

    public String GetServerIP2(){
        String hostIp = "" ;
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
                        hostIp =  address.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return hostIp;
    }
%>