views:

843

answers:

2

I would like to find out the ip address of the client that is visiting my web pages.

Content of JSP page:

<% 
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
+3  A: 
<% 
   out.print( request.getRemoteAddr() ); 
   out. print( request.getRemoteHost() ); 
%>
  • request.getRemoteAddr() return ip address of the machine from where you access the jsp page.
  • request.getRemoteHost() returns the name of host from which you are accessing the jsp page. 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:

<%
   out.print( request.getHeader("x-forwarded-for") );
%>
karim79
+1 for request.getHeader("x-forwarded-for")
Firstthumb
Does x-forwarded-for always work? Also, I don't think it will help if the client is on the other side of a device that does Network Address Translation.
John Saunders
Be aware that this is a non RFC standard header ("x-" prefix).
rodrigoap
@John Saunders - no, it does not always work. It depends on the proxy, the proxy may include the requesting client IP in that non-standard header.
karim79
<% out.print( request.getRemoteAddr() + "<br>"); out. print( request.getRemoteHost() ); %>Output:0:0:0:0:0:0:0:10:0:0:0:0:0:0:1I really do not know already, how do I determine the IP address of visitor ... :((((
wokena
+2  A: 

Your methods are correct. I assume that you are accessing it on localhost and therefore hitting the loopback interface. The numbers that you are seeing are the IPv6 IP addresses of your loopback interface.

Trying hitting it from another machine.

jsight
This is exactly the information I needed; thanks.
Sean Schulte