• 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, Domain using Java

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How can i get client's ip address,domain name etc?
I am using JSP,Javascript.
 
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
Have you examined the API for the request instance?
 
Usman Saeed
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i used etc but they give me IP address of either server or web host. I want information of Client's system
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getRemoteAddr, getRemoteHost and getRemotePort of ServletRequest returns details of the client calling the services. It can be the end client (request made directly from the client to server) or proxy (If there are proxy servers or NATting firewalls between your web server or servlet engine and the end users)
 
Usman Saeed
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply Fuzail but i have tested all of them, they return IP address of either web server or proxy server. How can i get Client machine's information. In aspx, it is possible but i can not find it in java
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the machine is behind a proxy you won't be able to get it's local IP or domain information, in any server side technology.
 
Usman Saeed
Ranch Hand
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about this:


 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The X-Forwarded-For (XFF) HTTP header is a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy Wiki

But if the request is coming directly from client then it can return null.

So you need to take care of this condition also
 
Usman Saeed
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply Fuzail,
I think i use following code
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mohd Fuzail:

But if the request is coming directly from client then it can return null.
So you need to take care of this condition also



Another point is that there is no guarantee that the proxy server will pass that header for you. So, the fact that the header is null, doesn't necessarily mean that the IP returned by getRemoteAddr is the actual IP of the machine that made the original request. It could still be the IP of a proxy server.

There is no absolute way to know the IP of the originating machine from a web app.
 
Usman Saeed
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your posts.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was used this code to get ip address but it was not given me the last address of client , i am using apache , please tell me what i will do...
thanks...
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My setup is like
Squid reverse-proxy -- sends request to--> Apache-server ---.--> JBoss app server (using mod_jk)

I want to get the clients IP address in my java application, running in JBoss.

In my apache access.log, I can see the clients IP address (as the apache LogFormat is using X-Forwaded-For)

But

String ipAddress = request.getHeader("X-FORWARDED-FOR");
in Java returns NULL.

What could be the issue?
Could I be missing something in my mod_jk configuration ?

thank you?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you must test your web app on you server, so the client's ip is as same as the server you were used.
 
Greenhorn
Posts: 13
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Try it
 
Ranch Hand
Posts: 57
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
request.getRemoteAddr() is the way. It appears your proxy changes the source IP. When some proxies do that they add the original IP in some custom http header. Use request.getHeaders() and print all of them to see if there isn't anything of interest. Like X-CLIENT-IP (made that one up, )
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question too.
How I can get the website ip that the http call came from. Let's say I have a jsonp call that calls a jsp file. How I can get the ip or url of the domain that this call came from and not the client/user information? Or is there any way to authenticate that the call came from a list of domains/ips in my database?
 
reply
    Bookmark Topic Watch Topic
  • New Topic