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
  header.
--%>
<c:if test="${param.language == 'en'}">
                  <fmt:setLocale value="en" scope="session" />
                  </c:if>
                  <c:if test="${param.language == 'sv'}">
                  <fmt:setLocale value="sv" scope="session" />
                  </c:if>
                  <c:if test="${param.language == 'de'}">
                  <fmt:setLocale value="de" scope="session" />
                  </c:if>
                  <fmt:setBundle basename="input" var="inputBundle" />
                  <fmt:setBundle basename="input" scope="session" />
<html>
  <head>
    <title>
      <fmt:message key="title" />
    </title>
  </head>
  <body bgcolor="white">
    <h1>
      <fmt:message key="title" />
    </h1>
  
    <fmt:message key="select_language" />
    <form action="input.jsp">
      <c:set var="currLang" value="${inputBundle.locale.language}" ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.