Example: Internationalization context in a servlet

The following code example illustrates how to use the internationalization context API within a servlet. Note comments in the init and doPost methods.

...
//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Imports.
//--------------------------------------------------------------------
import com.ibm.websphere.i18n.context.UserInternationalization;
import com.ibm.websphere.i18n.context.Internationalization;
import com.ibm.websphere.i18n.context.InvocationInternationalization;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Locale;

public class J2eeServlet extends HttpServlet {

  ...
  //------------------------------------------------------------------
  // INTERNATIONALIZATION SERVICE: API references.
  //------------------------------------------------------------------
  protected UserInternationalization userI18n = null;
  protected Internationalization i18n = null;
  protected InvocationInternationalization invI18n = null;

  //------------------------------------------------------------
  // INTERNATIONALIZATION SERVICE: JNDI name. 
  //------------------------------------------------------------
  public static final String UserI18NUrl = 
      "java:comp/websphere/UserInternationalization";  

  protected Locale callerLocale = null;
  protected Locale invocationLocale = null;

  /**
   * Initialize this servlet.
   * Resolve references to the JNDI initial context and the
   * internationalization context API.
   */
  public void init() throws ServletException {

    //------------------------------------------------------------------
    // INTERNATIONALIZATION SERVICE: Resolve API.
    //
    // Under Container-managed Internationalization (CMI), servlets have 
    // read-only access to invocation context elements. Attempts to set these 
    // elements result in an IllegalStateException.
    //
    // Suggestion: cache all internationalization context API references 
    // once, during initialization, and use them throughout the servlet 
    // lifecycle.
    //------------------------------------------------------------------
    try {
      Context initialContext = new InitialContext();
      userI18n = (UserInternationalization)initialContext.lookup(UserI18nUrl);
      callerI18n = userI18n.getCallerInternationalization();
      invI18n = userI18n.getInvocationInternationalization();
    } catch (NamingException ne) {
      throw new ServletException("Cannot resolve UserInternationalization" + ne);
    } catch (IllegalStateException ise) {
      throw new ServletException ("Error: UserInternationalization is not
         available: " + ise);
    }
    ...
  } // init
  
  /**
   * Process incoming HTTP GET requests.
   * @param request Object that encapsulates the request to the servlet
   * @param response Object that encapsulates the response from the
   *    Servlet.
   */
  public void doGet(
      HttpServletRequest  request, 
      HttpServletResponse response)
    throws ServletException, IOException {
    doPost(request, response);
  } // doGet

  /**
   * Process incoming HTTP POST requests
   * @param request Object that encapsulates the request to 
   *    the Servlet.
   * @param response Object that encapsulates the response from
   *    the Servlet.
   */
  public void doPost(
      HttpServletRequest  request,
      HttpServletResponse response)
    throws ServletException, IOException {

    ...
    //--------------------------------------------------------------------
    // INTERNATIONALIZATION SERVICE: Get caller context.
    //
    // The internationalization service extracts the accept-languages
    // propagated in the HTTP request and associates them with the 
    // current thread as a list of locales within the caller context.
    // These locales are accessible within HTTP Servlet service methods 
    // using the caller internationalization object.
    // 
    // If the incoming HTTP request does not contain accept languages,
    // the service associates the server's default locale. The service
    // always associates the GMT time zone.
    //
    //--------------------------------------------------------------------
    try {
      callerLocale = callerI18n.getLocale();   // caller locale
      // the following code enables you to get invocation locale,
      // which depends on the Internationalization policies.
      invocationLocale = invI18n.getLocale();  // invocation locale
    } catch (IllegalStateException ise) {
      log("An anomaly occurred accessing Invocation context: " + ise);
    }
    // NOTE: Browsers may propagate accept-languages that contain a 
    // language code, but lack a country code, like "fr" to indicate 
    // "French as spoken in France."  The following code supplies a
    // default country code in such cases.
    if (callerLocale.getCountry().equals(""))
      callerLocale = AccInfoJBean.getCompleteLocale(callerLocale);
    
    // Use iLocale in JDK locale-sensitive operations, etc.
    ...
  } // doPost
  
  ...
  void log(String s) {
    System.out.println (((s == null) ? "null" : s));
  }
} // CLASS J2eeServlet