The example following suggests how to utilize the Internationalization
context application programming interface (API) within a servlet. Note the init() and doPost() methods.
...
//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Imports.
//--------------------------------------------------------------------
import com.ibm.websphere.i18n.context.*;
public class J2eeServlet extends HttpServlet {
...
//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: API references.
//--------------------------------------------------------------------
UserInternationalization userI18n = null;
Internationalization callerI18n = null;
final String UserI18nUrl =
"java:comp/websphere/UserInternationalization";
...
/**
* Initialize this Servlet.
* <p>Resolve references to the JNDI initial context and the
* Internationalization context API.
*/
public void init() throws ServletException {
// Resolve the JNDI initial context.
try {
// JNDI requires the name of the naming context provider and
// the name of initial context factory. We store these names
// in a properities file.
Properties properties = new Properties();
properties.put("java.naming.provider.url", "iiop:///");
initialContext = new InitialContext(properties);
} catch (NamingException ne) {
throw new ServletException("Cannot resolve JNDI
initial conext:" + ne);
}
...
//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Resolve API.
//
// Under the Server-side Internationalization (SSI) context
// management policy, Servlets have read-only access to invocation
// context elements. Attempts to set these elements result in an
// IllegalStateException. And because Servlets “RunAsCaller”
// under SSI, the invocation and caller contexts are identical.
// So, this example resolves only a reference to the
// Internationalization interface to get caller locale and
// time zone.
//
// Suggestion: cache all Internationalization context API
// references once, during initialization, and use them
// throughout the Servet lifecycle.
//--------------------------------------------------------------------
try {
userI18n =
(UserInternationalization)initialContext.lookup(UserI18nUrl);
callerI18n = userI18n.getCallerInternationalization ();
} catch (NamingException e) {
throw new ServletException("Cannot resolve
UserInternationalization" + e);
} catch (IllegalStateException e) {
throw new ServletException("Cannot resolve
CallerInternationalization" + e);
}
...
} // 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 HTTP Browser Client Locales.
//
// The Internationalization service associates the locale list
// propagated in the HTTP request header with the current thread.
// It is accessible within HTTP Servlet service and subsequent
// methods via the Internationalization context API.
//
// If the incoming HTTP request does not contain a locale list,
// the Internationalization service associates the server’s default
// locale with the current thread. The service also associates the
// server’s default time zone.
//
// All context elements associate with the current thread will
// propagate over subsequent remote bean methods calls.
//--------------------------------------------------------------------
iLocale = callerI18n.getLocale();
// <WORKAROUND> Browsers are inconsistent regarding how they
// propagate locale information in that they may send locales
// containing a language code, but lacking a country code -
// like ("fr", "") for "French as spoken in France." The
/// following code supplies a default country code in these cases.
if (iLocale.getCountry().equals(""))
iLocale = customerLocale.getCompleteLocale(iLocale);
// </WORKAROUND>
// Use iLocale in JDK locale-sensitive operations, etc.
...
} // doPost
...
} // CLASS J2eeServlet