简体   繁体   中英

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

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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM