Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get client country name?

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Using request.getLocale().getDisplayCountry() method, i was expecting the country name of the client machine. But its returning US for the client machine located in India. What is the correct approach to get country name of the client machine? please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have the locale set to in the browser?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18014
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JavaDocs on ServletRequest:

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the country name of the client you're going to need access to an IP geolocation database. Then you can take the IP address of the request and look that up. But if you don't have access to such a service there's not much you can do. I'd suggest Googling for a service that meets your needs (most are commercial but there may be some free ones available).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends. Does the OP want the actual location, or the browser locale? They're very different things.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much experts!

now i understood that getLocale is not what i want. it will only check client browser settings or Accept-Language header to get the locale (thanks Bear and Tim for this info).

Let me elaborate my problem and please let me know, if there any standard solution to the problem. I am developing a web-site and have a User Registration page. i want to set the country field to the country name of the User by default.

i searched on the net for free IP geolocation databases (thanks Matthew for this info). but, this service is provided by many sites. i am not able to assess which one is reliable and compatible with java. please suggest me one of the reliable sites. Thank you.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18014
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IP Geolocation is commonly provided by either a web service or REST, so it's easy to code up Java clients that can send an IP out and get back a location.

As far as which one to use, I generally recommend looking at the ones that are relatively close to your server, since they're typically going to respond faster and require fewer Internet resources. You might want to poll several different ones, even.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No such service is perfect, especially not if it's free. I'd recommend to try the ones you're considering with a set of typical IPs you're likely to see from your users.

Another option is to use a service you can run completely on your server, thus obviating the need for remote calls. MaxMind has such a free version with a Java API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic