Recipe 24.1 Detecting the Client Locale in a Servlet


You want to detect the client locale in a servlet.


Use the ServletRequest.getLocale( ) method.


The locale is represented by a class in Java: java.util.Locale . The ServletRequest object can access the client's "preferred" locale with its getLocale( ) method, which returns a Locale object.

The preferred locale is the user's top preference. For example, a user may configure their browser with a Spanish language locale ("es_ES") as the preferred one.

Java code can access the list of locales that a user configures a browser with by calling ServletRequest's getLocales( ) method, which returns an Enumeration object. This object contains the preferred and less-preferred locales.

To set the language preference(s) in Netscape 7.1, go to "Edit Preferences Netscape Languages." In the Macintosh Safari browser, open System Preferences and drag your language preference(s) to the top of the list (then restart Safari). In Internet Explorer 5.5, go to "Tools Internet Options Languages."

Example 24-1 accesses the client's preferred locale by calling request.getLocale( ) . The servlet then displays information about the locale by calling some Locale methods . Example 24-1 also displays infomation about the less-preferred locales by using the method request.getLocales( ) .

Example 24-1. Accessing the Locale object in a servlet
 package com.jspservletcookbook;  import java.util.Enumeration; import java.util.Locale;  import javax.servlet.*; import javax.servlet.http.*; public class LocaleDisplay extends HttpServlet {   public void doGet(HttpServletRequest request,      HttpServletResponse response)     throws ServletException, {  //Get the client's Locales       Enumeration enum = request.getLocales( );       //Get the preferred Locale       Locale preferred = request.getLocale( );       String prefDisplay = "";       if (preferred != null)           prefDisplay = preferred.getDisplayName( );  //Display the preferred and any other locales       response.setContentType("text/html"); out = response.getWriter( );       out.println(         "<html><head><title>Locale Display</title></head><body>");              out.println("<h2>Here is your Locale info...</h2>");  out.println("<b>Preferred Locale:</b> ");       out.println( prefDisplay );  out.println("<br />");  out.println("Locale country: ");       if (preferred != null)          out.println( preferred.getDisplayCountry( ) );  out.println("<br />");  out.println("Locale language: ");       if (preferred != null)           out.println( preferred.getDisplayLanguage( ) );       out.println("<br /><br />");       out.println("<h3>Lower priority Locales...</h3>");       Locale loc = null;       while (enum.hasMoreElements( )){           loc = (Locale)enum.nextElement( );           if (! (loc.getDisplayName( ).equals( prefDisplay ))){               out.println("Locale: ");               out.println( loc.getDisplayName( ) );               out.println("<br />");               out.println("Locale country: ");               out.println( loc.getDisplayCountry( ) );               out.println("<br />");               out.println("Locale language: ");               out.println( loc.getDisplayLanguage( ) );               out.println("<br /><br />");           }//if       }//while  out.println("</body></html>");          } //doGet } 

Figure 24-1 shows the web browser output when a visitor with a preferred locale of "en_US" requests the servlet.

Figure 24-1. The servlet displays the preferred and less-preferred locales

This user has configured their browser with several other locales. As you can see, the method locale.getDisplayName( ) is designed to return a more readable name (compared with "de_CH") for the locale.

The com.oreilly.servlet library includes the LocaleNegotiator class, which uses the client request to figure out the best charset, locale, and resource bundle to use with the response. See Recipe 8.4 for tips on using the com.oreilly.servlet library. I don't cover LocaleNegotiator in particular here, but the Javadoc explains this class. See

See Also

The Javadoc describing the Locale class:; Recipe 24.2 on detecting the locale using a JSP.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: