aspose file tools*
The moose likes JSP and the fly likes How to get client's IP address, Domain using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get client Watch "How to get client New topic
Author

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

Usman Saeed
Ranch Hand

Joined: May 21, 2008
Posts: 34
Hello,
How can i get client's ip address,domain name etc?
I am using JSP,Javascript.


Usman Saeed
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

Have you examined the API for the request instance?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Usman Saeed
Ranch Hand

Joined: May 21, 2008
Posts: 34
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

Joined: Feb 20, 2002
Posts: 107
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)


Being defeated is often a temporary condition. Giving up is what makes it permanent.
Usman Saeed
Ranch Hand

Joined: May 21, 2008
Posts: 34
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Usman Saeed
Ranch Hand

Joined: May 21, 2008
Posts: 34
what about this:


Mohd Fuzail
Ranch Hand

Joined: Feb 20, 2002
Posts: 107
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

Joined: May 21, 2008
Posts: 34
Thanks for reply Fuzail,
I think i use following code
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: May 21, 2008
Posts: 34
Thank you all for your posts.
satish sas
Greenhorn

Joined: Dec 02, 2008
Posts: 2
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

Joined: Jul 03, 2007
Posts: 77
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

Joined: Sep 09, 2011
Posts: 5
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

Joined: Nov 22, 2011
Posts: 13


Try it

If we can only encounter each other rather than stay with each other, then I wish we had never encountered.
Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

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

Joined: Jan 04, 2012
Posts: 1
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?
 
 
subject: How to get client's IP address, Domain using Java