如何用Java和jsp获取服务器ip

Java中:

1、获取服务端ip

String ip = InetAddress.getLocalHost().getHostAddress();

2、获取客户端本机ip

String remoteAddr = request.getRemoteAddr();  
String forwarded = request.getHeader("X-Forwarded-For");  
String realIp = request.getHeader("X-Real-IP");  
String ip = null;  
if (realIp == null) {  
    if (forwarded == null) {  
        ip = remoteAddr;  
    } else {  
        ip = remoteAddr + "/" + forwarded.split(",")[0];  
    }  
} else {  
    if (realIp.equals(forwarded)) {  
        ip = realIp;  
    } else {  
        if(forwarded != null){  
            forwarded = forwarded.split(",")[0];  
        }  
        ip = realIp + "/" + forwarded;  
    }  
}
jsp中:

<%@page import="java.net.InetAddress"%>
<%
String ip = InetAddress.getLocalHost().getHostAddress();
%>
输出<%=ip%>


你可能感兴趣的:(java,jsp,服务器,jsp,java)