2010-07-05 194 views

回答

10

其實,對於服務器的IP地址,您需要使用

String serverIP = request.getLocalAddr(); 
+0

謝謝秒。爲什麼我在javadoc上看不到? @ _ @ @ – portoalet 2010-07-05 04:40:20

+0

@portoalet:可能沒有俯視它。爲2.4版本的servlet規範添加了getLocalAddr()(我相信Google中的第一個結果是Java EE 1.3的Javadoc) – ig0774 2010-07-05 04:48:28

+0

好吧,解釋它。 – portoalet 2010-07-05 04:50:51

3
String sIPAddr = request.getRemoteAddr(); 
2
String addr = request.getRemoteAddr(); 
0
request.getHeader("X_FORWARDED_FOR") 
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 %>