0

I want to fetch Ip address of client in my jsp page and want to store on my Database.

I am using following code in Jsp page to fech Ip address, but it is showing the servers Ip address.

 <input type="hidden" name="ipaddress" value="<%=request.getRemoteAddr()%>"/> 
Roman C
  • 49,761
  • 33
  • 66
  • 176
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

2 Answers2

9

In usual case the method ServletRequest.getRemoteAddr() (or getRemoteHost() and getRemotePort()) should returns details of the actual client. But, if its routed through a proxy, or a differnt servlet engine you would be getting ip of those machines. To tackle these situation you could try below operations

request.getHeader("VIA")             --> Gateway   
request.getHeader("X-FORWARDED-FOR")--> IPaddress

But this could return null if the request is directly from the client. You may handle the situation with some additional conditions to get valid data.

Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
1

Trying to get the client IP address from the server side with the request object is never a reliable method.

The most reliable ones tend to get executed on the client side, with ActiveX, Applets, or Javascript just to name a few. However, this also poses some challenges. Check the answers to this question just so you know what you're up against (This is my prefered answer from that)

Community
  • 1
  • 1
betomontejo
  • 1,657
  • 14
  • 26