简体   繁体   English

如何确定Web客户端的IP地址(对于JSP)?

[英]How do I determine the IP address of a web client (for a JSP)?

I would like to find out the ip address of the client that is visiting my web pages. 我想找出访问我网页的客户端的IP地址。

Content of JSP page: JSP页面的内容:

<% 
out.print( request.getRemoteAddr() + "<br>");
out.print( request.getRemoteHost() ); 
%> 

Output: 输出:

0:0:0:0:0:0:0:1
0:0:0:0:0:0:0:1
<% 
   out.print( request.getRemoteAddr() ); 
   out. print( request.getRemoteHost() ); 
%>
  • request.getRemoteAddr() return ip address of the machine from where you access the jsp page. request.getRemoteAddr()返回访问jsp页面的机器的IP地址。
  • request.getRemoteHost() returns the name of host from which you are accessing the jsp page. request.getRemoteHost()返回您从中访问jsp页面的主机名。 If you access it from server itself, it will return server name. 如果从服务器本身访问它,它将返回服务器名称。

If the client is behind a proxy, the above are not useful as you will get the IP of the proxy they are behind, instead try: 如果客户端位于代理服务器后面,则上述操作无效,因为您将获得他们所支持的代理的IP,而是尝试:

<%
   out.print( request.getHeader("x-forwarded-for") );
%>

Your methods are correct. 你的方法是正确的。 I assume that you are accessing it on localhost and therefore hitting the loopback interface. 我假设你在localhost上访问它,因此命中了loopback接口。 The numbers that you are seeing are the IPv6 IP addresses of your loopback interface. 您看到的数字是您的环回接口的IPv6 IP地址。

Trying hitting it from another machine. 尝试从另一台机器上击中它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM