Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Advanced question (I think)  RSS feed

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers.
I want to develope a servlet that can know the IP of the client and determin to which country he is belong.
I am thinking to specify the proxy name (if possible) , so if it is ends with uk so the user from england and so on....
any ideas ...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an accept-language header which forms part of HTTP. From this you can get the prefered Locale of the client, presuming the request includes this header. Of course there is not guarentee the Locale a user sets their machine to maps to the actualy country they are in.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first part (retrieving the IP address) is easy, just call "getRemoteAddr()" on the request.
The second part is tricky to say the least.
There is no real geographic split in IP addresses anymore. Though in theory blocks are assigned to regional entities this doesn't suffice for your purpose as there are super-regional customers to those entities.

For example, AOL may request a range of IP addresses from the US pool and assign those to German customers. If you used the pool from which they are drawn as a basis you'd class that German as an American.
The pools are also not fine-grained enough for your purpose. All of Europe shares a single agency for handing out blocks for example.

More information on IP assignments: http://www.ripe.net/ripencc/about/regional/

You could try a whois lookup on the number and try to determine where it is assigned to.
But that still would not necessarilly resolve scenarios where an IP address is assigned to a company in one country but used by a computer located in another.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
There is an accept-language header which forms part of HTTP. From this you can get the prefered Locale of the client, presuming the request includes this header. Of course there is not guarentee the Locale a user sets their machine to maps to the actualy country they are in.


none at all indeed.
For example I'm definitely not in the USA or UK but I have my preferences set to UK English first, US English second and Dutch third.
That would make
EN_en
EN_uk
EN_us
NL_nl
NL_be
I think I send German (DE_de, DE_at) as well.

Most people never change those settings at all, and unless they have a localised OS and browser for not just their language but country incorrect data will be returned.
 
Mike Fuellbrandt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try MaxMin GeoIP
I haven't played around with it, but it's got a java api and free download.

You can see it in action at the at:MySql downloads page

Of course, this is IP is not guaranteed to be accurate, as per the posts above.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To translate an IP address to a host name, you need to do a "reverse lookup". The sockets API to do this is:
gethostbyaddr ()

If the resulting TLD is something like ".uk" or ".de", then you're in luck. Otherwise, you'll have to fall back on some other method (like parsing the language info in the HTTP header).

Hope that helps .. PSM
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!