wokena
wokena

Reputation: 1359

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

Upvotes: 5

Views: 7476

Answers (2)

jsight
jsight

Reputation: 28429

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.

Upvotes: 6

karim79
karim79

Reputation: 342745

<% 
   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") );
%>

Upvotes: 6

Related Questions