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.
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.
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).
It depends. Does the OP want the actual location, or the browser locale? They're very different things.
Joined: Jun 28, 2007
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.
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.
Joined: Mar 22, 2005
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.