[英]Get Server IP address from JSP Request/session object
How can I get the IP address of the server from a JSP page? 如何从JSP页面获取服务器的IP地址?
Right now, all I can do is request.getLocalName(), which returns the server name, not the IP address? 现在,我所能做的就是request.getLocalName(),它返回服务器名称,而不是IP地址?
实际上,对于服务器的IP地址,您需要使用
String serverIP = request.getLocalAddr();
String sIPAddr = request.getRemoteAddr();
To get an actual server IP and hostname (actual and not set by eg a proxy) use this: 要获取实际的服务器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 %>
String addr = request.getRemoteAddr();
request.getHeader("X_FORWARDED_FOR")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.