Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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
like image 570
wokena Avatar asked Aug 11 '09 18:08

wokena


2 Answers

<% 
   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") );
%>
like image 75
karim79 Avatar answered Nov 14 '22 21:11

karim79


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.

like image 33
jsight Avatar answered Nov 14 '22 22:11

jsight