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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get Client Watch "How to get Client New topic
Author

How to get Client's IP address in Servlet

Joe Minhan
Greenhorn

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) {
java.net.InetAddress netip = java.net.InetAddress.getLocalAddress();
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.

Regards

Matt Gaunt
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16142
    
  21

It's worse than that. My IP address is 192.168.100.2. 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
Greenhorn

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

Joined: Jan 10, 2002
Posts: 61426
    
  67

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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get Client's IP address in Servlet