[ Team LiB ] Previous Section Next Section

Recipe 24.2 Detecting the Client's Locales in a JSP

Problem

You want to find out what the request's preferred locale and less-preferred locales are and display this information in a JSP.

Solution

Grab the preferred locale with the expression "${pageContext.request.locale}." Get access to all of the locales with the expression "${pageContext.request.locales}."

Discussion

The JSTL tags make it easy to adapt a JSP for visitors who speak different languages. Example 24-2 uses the EL to create a variable named clientLocale that represents the request's preferred locale. Then the JSP displays the locale's name, language, and country. Example 24-2 also displays any information about the client's less-preferred locales.

Example 24-2. Accessing the request's locale in a JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head><title>Locale Display</title></head>
<body>
<h2>Here is your preferred locale info...</h2>

<c:set var="clientLocale" value="${pageContext.request.locale}" />
<c:set var="clientLocales" value="${pageContext.request.locales}" />

Preferred locale: ${clientLocale.displayName}
 <br />
Preferred locale country: ${clientLocale.displayCountry}
<br />
Preferred locale language: ${clientLocale.displayLanguage}
<h3>Lower priority locales...</h3>
<c:forEach var="loc" items="${clientLocales}" begin="1">
    Locale: ${loc.displayName}
    <br />
     Locale country: ${loc.displayCountry}
         <br />
    Locale language: ${loc.displayLanguage}
        <br /><br />
</c:forEach>

</body>
</html>

The expression "${pageContext.request.locale}" gets the request object from the pageContext implicit object, then accesses the Locale object, representing the client's preferred locale, from the request. Fairly efficient, huh? Then an expression such as "${clientLocale.displayName}" represents the equivalent of calling the Locale object's getDisplayName( ) method.

The EL phrase "${pageContext.request.locales}" represents the equivalent of calling the ServletRequest object's getLocales( ) method, which returns a java.util.Enumeration type. The Enumeration contains all of the client's configured locales, begining with their preferred locale.

Example 24-2 uses an implmentation of JSTL 1.1 and JSP 2.0. Make sure to include the proper taglib directive at the top of the JSP file, so that the JSP can use the EL and core tags. If all this terminology is new and strange to you, read Chapter 23 on the JSTL.


Example 24-2 iterates through each of the locales using the c:forEach tag, as in:

<c:forEach var="loc" items="${clientLocales}" begin="1">

The begin="1" attribute begins the c:forEach iteration with the second locale object, since the first locale is the client's preferred one, and the JSP has already displayed information on that one. The begin attribute uses "0" as the index for the first item in the Enumeration.

Figure 24-2 shows the JSP's web browser output. This output results from a visitor whose browser specifies the locale "es_ES" as preferred.

Figure 24-2. A browser requests a locale-sensitive JSP
figs/jsjc_2402.gif

See Also

Chapter 23 on the JSTL; the Javadoc describing the Locale class: http://java.sun.com/j2se/1.4.1/docs/api/java/util/Locale.html.

    [ Team LiB ] Previous Section Next Section