aspose file tools
The moose likes JSP and the fly likes How to get Client's IP address in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get Client Watch "How to get Client New topic

How to get Client's IP address in Servlet

Joe Minhan

Joined: Oct 27, 2008
Posts: 2
I use Microsoft Server 2003, Appache Tomcat 6.x, and ISA firewall. ISA firewall has redirection rule. I wrote my code is:
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if(ipAddress == null) { netip =;
ipAddress = netip.getHostAddress();

the result is only the server address. I want to get client's IP address. Please send to me some code for that.
Matt Gaunt
Ranch Hand

Joined: Sep 05, 2003
Posts: 34
Have you tried request.getRemoteAddr()

This will work assuming your end user is not going through a proxy. And if they are connecting via a proxy server, then that proxy server should not be stripping the remote ip address from the request object.

Obtaining the ip address is not simple using the j2ee request object as the availability of that information depends on the proxy server which redirects the end users request.


Matt Gaunt
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16542

It's worse than that. My IP address is So is the IP address of 1000s of other people with home routers equipped with NAT. If the server captured my IP address it would be the NAT address, which is the same address for every computer on my LAN.

IP addresses are not a viable way to identify specific computers.

Customer surveys are for companies who didn't pay proper attention to begin with.
Joe Minhan

Joined: Oct 27, 2008
Posts: 2
Yes, I already try request.getRemoteAddr(). but it does not work.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62312

Originally posted by Joe Minhan:
but it does not work.

Did it cause your computer to burst into flames?
[ October 28, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Have you tried LearnNowOnline?
subject: How to get Client's IP address in Servlet