• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get client's IP address, Domain using Java

 
Usman Saeed
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
How can i get client's ip address,domain name etc?
I am using JSP,Javascript.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
what about this:


 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for reply Fuzail,
I think i use following code
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all for your posts.
 
satish sas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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...
 
Jeevan Sunkersett
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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?
 
Lei XianXiang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try it
 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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, )
 
John Maris
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic