This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to get Client's IP address in Servlet

 
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.
 
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
 
Saloon Keeper
Posts: 21240
137
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
Marshal
Posts: 67295
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 ]
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!