Recipe 24.5 Using the ResourceBundle in a Servlet


Problem

You want a servlet to dynamically display a "Welcome" message to visitors depending on their locale.

Solution

Use the servlet to access the translated text dynamically from a ResourceBundle .

Discussion

Once you have added ResourceBundles to the web application, then servlets can use them to dynamically display text based on the user 's locale.

Remember, the web application stores ResourceBundles as .properties files (text) or Java classes.


Example 24-6 uses a ResourceBundle with a basename of "WelcomeBundle." It is stored in WEB-INF/i18n , so its fully qualified name is i18n.WelcomeBundle .

Example 24-6. A servlet uses a to dynamically display translated text
 package com.jspservletcookbook;  import java.util.Locale; import java.util.ResourceBundle;  import javax.servlet.*; import javax.servlet.http.*;  public class WelcomeServlet extends HttpServlet {  public void doGet(HttpServletRequest request,      HttpServletResponse response) throws ServletException,     java.io.IOException {  //Get the client's Locale       Locale locale = request.getLocale( );       ResourceBundle bundle = ResourceBundle.getBundle(         "i18n.WelcomeBundle",locale);       String welcome =  bundle.getString("Welcome");  //Display the locale       response.setContentType("text/html");       java.io.PrintWriter out = response.getWriter( );       out.println("<html><head><title>"+welcome+"</title></head><body>");  out.println("<h2>"+welcome+"</h2>");  out.println("Locale: ");       out.println( locale.getLanguage( )+"_"+locale.getCountry( ) );              out.println("</body></html>");             } //end doGet   // doPost method ...  }//WelcomeServlet 

Here is how the application uses this resource in response to a visitor from a Spanish locale ("es_ES"):

  1. The servlet accesses the locale as a java.util.Locale object.

  2. It passes the locale into the ResourceBundle.getBundle( ) method, which uses the locale to search for a ResourceBundle named i18n.WelcomeBundle_es_ES . The method forms this search term by attaching the current request's locale name to the end of the ResourceBundle basename. In this case, the bundle is stored as a Java class (Recipe 24.4).

  3. The bundle then displays the message by accessing the "Welcome" key, which is specified by the ResourceBundle (Example 24-4 or Example 24-5).

Sometimes the browser sends the locale information as a language code only, as in "es" for Spanish (instead of "es_ES" with language code and country code). If the application has only installed a resource named WelcomeBundle_es_ES, but not WelcomeBundle_es , then the getBundle( ) method defaults to a resource named WelcomeBundle (which might not be the optimal outcome), and therefore may not display the translated text. Make sure to include a WelcomeBundle_es resource to cover these cases.


Figure 24-3 shows the servlet's output in response to a request from a Spanish locale.

Figure 24-3. A Spanish client requests the LocaleServlet
figs/jsjc_2403.gif

Figure 24-4 shows the servlet's response when it deals with a locale for which the application has not provided a resource. In this case, the browser is set for the Japanese language, but the application has not yet provided a resource for this locale.

Figure 24-4. A browser set for Japanese visits receives the default message
figs/jsjc_2404.gif

The text that the browser displays derives from the default properties file: WelcomeBundle.properties (notice the absence of any locale- related suffix in the filename).

See Also

Recipe 24.6 on using the ResourceBundle in a JSP; The Javadoc for ResourceBundle : http://java.sun.com/j2se/1.4.1/docs/api/java/util/ResourceBundle.html.



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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net