• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Client's IP address in Servlet

 
Joe Minhan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18301
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Minhan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I already try request.getRemoteAddr(). but it does not work.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic