• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get Client's IP address in Servlet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I already try request.getRemoteAddr(). but it does not work.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic