Link to home
Start Free TrialLog in
Avatar of Micheal_Male
Micheal_MaleFlag for Afghanistan

asked on

Getting client remote address

Is there a way to get the client IP address when making  HTTP call ?. If a user types google.com i need to get the user IP address .
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Avatar of Micheal_Male

ASKER

The InetAddress.getLocalHost().getHostName() or .getAddress() gives me the server address or IP where my application is running not clients IP address who is making a call. Just like in access.log you see IP addresses logged in.

Creating a servlet will be of no use also because i would like to pass that IP Address to one of my method outside the servlet in a seperate JAR file which is deployed within the web application. I was looking for something without need of extending the HttpServlet class and writing doPost()method to get the IP address of the client.

 
The only way to get client's IP address is to use a servlet (or JSP): request.getRemoteAddr();
You can then pass it to another location as you need.
hmmm. But still that servlet will be called only if a user goes to that servlet URL which in my senario fails only if it is possible to have a servlet which is called everytime a user access the web app ? like a lisetener or a filter ?.
There are no JSP only third party servlet configurations.
Looks like a filter servlet is the best solution for you.
finally i found a way without creating a servlet after reading the spring docs.
A word of caution over here is that if the user is behind an ISP which is a fairly common thing then the HTTPServlet methods (and other methods relying on client calls) will return the ISP address and not the machine IP.

In a typical production scenario, when one of the banking sites was used from inside a closed organization, all the front-end users from different machines returned the same ISP host IPs.

In such a case, it is best to use an applet with call to InetAddress.getLocalHost().getHostName() etc.
Again this is not a very foolproof method and might require you to signed applet.

It is never a good and secure idea to get the users' local details.
Also it makes your application non-flexible.
I agree with you but the calls made to the http request are all going to be running on the same network but different terminals.
Just take an example of wallgreens who have a server. That server is connected to several cash registers. Eaxh cash register makes a call to the server to either store a transaction etc. So if you use inetaddresss.getlocalhost then it returns the server ip address not the terminal ip who made the call to the server.
ASKER CERTIFIED SOLUTION
Avatar of calboronster
calboronster
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Applet is not a solution for me but thanks for your input. I got your point what you are trying to say.
Nice explanation and suggestion.