[ Team LiB ] |
![]() ![]() |
Recipe 24.1 Detecting the Client Locale in a ServletProblemYou want to detect the client locale in a servlet. SolutionUse the ServletRequest.getLocale( ) method. DiscussionThe 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.
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. 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 servletpackage 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, java.io.IOException { //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"); java.io.PrintWriter 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.
See AlsoThe Javadoc describing the Locale class: http://java.sun.com/j2se/1.4.1/docs/api/java/util/Locale.html; Recipe 24.2 on detecting the locale using a JSP. |
[ Team LiB ] |
![]() ![]() |