Handling Localized Input

So far we have discussed how to generate pages in different languages, but most applications also need to deal with localized input. As long as you’re supporting only Western European languages, the only thing you typically need to worry about is how to interpret dates and numbers. The JSTL I18N actions can help you with this as well.

Example 13-5 shows a JSP page with the same form for selecting a language as in Example 13-1, plus a form with one field for a date and another for a number.

Example 13-5. Date and number input form (input.jsp)

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
  Set the locale to the selected one, if any. Otherwise, let the
  <fmt:bundle> action pick the best one based on the Accept-Language
<c:if test="${param.language == 'en'}">
                  <fmt:setLocale value="en" scope="session" />
                  <c:if test="${param.language == 'sv'}">
                  <fmt:setLocale value="sv" scope="session" />
                  <c:if test="${param.language == 'de'}">
                  <fmt:setLocale value="de" scope="session" />
                  <fmt:setBundle basename="input" var="inputBundle" />
                  <fmt:setBundle basename="input" scope="session" />
      <fmt:message key="title" />
  <body bgcolor="white">
      <fmt:message key="title" />
    <fmt:message key="select_language" />
    <form action="input.jsp">
      <c:set var="currLang" value="${inputBundle.locale.language}" ...

Get JavaServer Pages, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.