Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5837919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:24:45+00:00 2026-05-22T11:24:45+00:00

I have to detect user country and language automatically in Java Servlet using request

  • 0

I have to detect user country and language automatically in Java Servlet using request details (IP address, browser information etc.). Is it possible to detect these settings for the most of users (~90%)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:24:46+00:00Added an answer on May 22, 2026 at 11:24 am

    Detecting the language

    Detecting the correct language is easy. Web browsers tend to send AcceptLanguage header and Java Servlet API is so nice to actually convert it contents to Locale object(s). All you would have to do, is just access this information and implement fall-back mechanism. To do that you actually need a list of Locales your application is going to support (you could think of creating some sort of Properties file that would contain supported locales along with default one). Example below shows such implementation:

    public class LousyServlet extends HttpServlet {
        private Properties supportedLanguages;
        private Locale requestLocale = (Locale) supportedLanguages.get("DEFAULT");
    
        public LousyServlet() {
            supportedLanguages = new Properties();
            // Just for demonstration of the concept
            // you would probably load it from i.e. XML
            supportedLanguages.put("DEFAULT", Locale.US);
            // example mapping of "de" to "de_DE"
            supportedLanguages.put("de-DEFAULT", Locale.GERMANY);
            supportedLanguages.put("de_AT", new Locale("de", "AT"));
            supportedLanguages.put("de_CH", new Locale("de", "CH"));
            supportedLanguages.put("ja_JP", Locale.JAPAN);
        }
    
        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            detectLocale(request);
    
            super.doGet(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            detectLocale(request);
    
            super.doPost(request, response);
        }
    
        private void detectLocale(HttpServletRequest request) {
            Enumeration locales = request.getLocales();
            while (locales.hasMoreElements()) {
                Locale locale = (Locale) locales.nextElement();
                if (supportedLanguages.contains(locale)) {
                    requestLocale = locale;
                    break;
                }
            }
        }
    
        public String getLanguage() {
            // get English name of the language
            // For native call requestLocale.getDisplayName(requestLocale)
            return requestLocale.getDisplayLanguage();
        }
    }
    

    Mind you, that you would need to list all the countries for given language, as it won’t fall back in this case. That is for the reason. Local users tend to have non-specific Locale either way (for example my web browser sends pl_PL, pl, en_US, en in that order). And the reason is, there are some languages that differs substantially depending on the country, for example Brazilian Portuguese is different than Portuguese and Chinese Traditional (Taiwan, Hong Kong) is different than Chinese Simplified (China, Singapore) and it won’t be appropriate to fall back to one of them.

    Detecting country

    Depending on what you need this information for, it might or might not be straightforward. If end user’s web browser is configured correctly, it will give you the hint of end user’s preferred location – that would be the part of Locale. If you only need that information to decide on which localized page to load, that would be probably the best option. Of course if Locale object is not specific (country-less) you may want to assign “default” country for each supported non-specific Locale. In both cases, you should provide end user with some means of switching country (i.e. through “Other countries” combo box). The list could be obtained like this:

    public String[] getOtherCountries() {
        Set<String> countries = new HashSet<String>();
        Set<Object> keys = supportedLanguages.keySet();
        for (Object key : keys) {
            Locale other = (Locale) supportedLanguages.get(key);
            if (other != requestLocale) {
                countries.add(other.getDisplayCountry(requestLocale));
            }
        }
    
        return countries.toArray(new String[0]);
    }
    

    If however, you need this to restrict the access to the contents based on location, the problem is harder. You may think of checking the IP. You would need to prepare some Database with address classes that belongs to given country. This data could be found on the Internet. The only problem with that solution is, user may configure a web proxy and fool your web site on his real location. Also, corporate users might appear as if they connect from USA where in fact they connect from UK or Ireland. Either way, it is your best shot.

    There was some question on GeoLocation before and I believe you may find it useful. Here you are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to detect in my flash if the user closes his browser or
I have an implementation of javax.servlet.http.HttpSessionListener that is supposed to detect user session invalidation/timeout
I have a batch file that will detect if the user has the .Net
I have a UIImagePickerController subclass, and I would like to detect when the user
I have a couple of simple PHP functions I am using. One to detect
Is it possible to detect when the user clicks on the browser's back button?
Suppose I have one div in my page. how to detect the user click
I have a PHP script where I'd like to detect if the user is
I have an e-commerce site and I'd like to check which country a user
i have a windows service that get user details and save the result into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.