如何从JSP页面获取服务器的IP地址?
现在,我所能做的就是request.getLocalName(),它返回服务器名称,而不是IP地址?
答案 0 :(得分:10)
实际上,对于服务器的IP地址,您需要使用
String serverIP = request.getLocalAddr();
答案 1 :(得分:3)
String sIPAddr = request.getRemoteAddr();
答案 2 :(得分:2)
String addr = request.getRemoteAddr();
答案 3 :(得分:2)
要获取实际的服务器IP和主机名(实际而不是由代理设置),请使用以下命令:
<%@ page import="java.net.*" %>
[...]
<%
String hostname, serverAddress;
hostname = "error";
serverAddress = "error";
try {
InetAddress inetAddress;
inetAddress = InetAddress.getLocalHost();
hostname = inetAddress.getHostName();
serverAddress = inetAddress.toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
%>
<li>InetAddress: <%=serverAddress %>
<li>InetAddress.hostname: <%=hostname %>
答案 4 :(得分:0)
request.getHeader("X_FORWARDED_FOR")