*
The moose likes Servlets and the fly likes how to get client country name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get client country name?" Watch "how to get client country name?" New topic
Author

how to get client country name?

vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191

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

Joined: Jan 10, 2002
Posts: 60782
    
  65

What do you have the locale set to in the browser?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Jan 10, 2002
Posts: 60782
    
  65

It depends. Does the OP want the actual location, or the browser locale? They're very different things.
vikas sharmaa
Ranch Hand

Joined: Jun 28, 2007
Posts: 191
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

Joined: Jun 25, 2001
Posts: 15957
    
  19

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
Marshal

Joined: Mar 22, 2005
Posts: 41109
    
  45
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.


Ping & DNS - my free Android networking tools app
 
 
subject: how to get client country name?
 
Similar Threads
Advanced question (I think)
how to get user name of the client machine
Getting the Client machine name
get client's system name
database connection