xrcwrn xrcwrn - 1 year ago 110
Java Question

Getting IP address of client in Jsp page

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()%>"/>

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download