Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Server-side Java: Internationalize JSP-based Websites

Build Websites that speak in tongues

  • Print
  • Feedback

Page 4 of 6

You will see that most GUI elements, including header messages, button labels, and so forth, are locale sensitive. They are set at runtime by interrogating the session for the appropriate resource:

<title>
<%=session.getValue("main.title")%>
</title>


Listing 4: eshop.jsp

<%@ page import="java.util.*, shopping.i18n.CD, java.text.*" %>
<%!
   public String computePrice(double price, HttpSession session) {
     String exchangeRate = (String)session.getValue("dollar.exchRate");
     price*= new Double(exchangeRate).doubleValue();
     NumberFormat form = NumberFormat.getCurrencyInstance((Locale)
                         session.getValue("myLocale"));
     return form.format(price);
   }
%>
<html>
<head>
<title>
<%=session.getValue("main.title")%>
</title>
</head>
<body bgcolor='<%=session.getValue("main.bgcolor")%>'>
<font face="Times New Roman,Times" size=+3>
<%=session.getValue("main.title")%>
</font>
<br>
<em>
<%=session.getValue("main.subhead")%>
</em>
<hr>
<p>
<center>
<form name="shoppingForm" action="main.jsp" method="post"
CD:
<select name=CD>
<option>A Toda Cuba le Gusta| Afro-Cuban All Stars | Cuba | <%=computePrice(16.95,session)%></option>
<option>Vujicsics | Vujicsics | Croatia | <%=computePrice(14.95,session)%></option>
<option>Kaira | Tounami Diabate| Mali | <%=computePrice(16.95,session)%></option>
<option>The Lion is Loose | Eliades Ochoa | Cuba | <%=computePrice(13.95,session)%></option>
<option>Dance the Devil Away | Outback | Australia | <%=computePrice(14.95,session)%></option>
<option>Record of Changes | Samulnori | Korea | <%=computePrice(12.95,session)%></option>
<option>Djelika | Tounami Diabate | Mali | <%=computePrice(14.95,session)%></option>
<option>Wimme | Wimme | Finland | <%=computePrice(12.95,session)%></option>
<option>Cesaria Evora | Cesaria Evora | Cape Verde | <%=computePrice(16.95,session)%></option>
<option>Ibuki | Kodo | Japan | <%=computePrice(13.95,session)%></option>
</select>
<%=session.getValue("main.qtyLabel")%>: 
<input type="text" name="qty" size="3" value="1">
<input type="hidden" name="action" value="ADD">
<input type="submit" name="Submit"
 value='<%=session.getValue("main.addLabel")%>'>
</form>
</center>
<p>
<jsp:include page="cart.jsp" flush="true" />
</body>
</html>


Also, notice that another JSP page, cart.jsp (shown in Listing 5), is included within eshop.jsp via the following directive:

<jsp:include page="cart.jsp" flush="true" />


Here, cart.jsp handles the presentation of the session-based shopping cart, which is implemented by a Vector object. Observe the scriptlet at the beginning of cart.jsp:

<%
  Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
  if (buylist != null && (buylist.size() > 0)) {
%>


Basically, the scriptlet extracts the shopping cart from the session. If the cart is empty or not yet created, it displays nothing; thus, the first time users access the application, they are presented with the view shown in Figure 2, assuming the users had opted to localize the site for French.

Figure 2. Main view localized for French

Listing 5: cart.jsp

<%@ page import="java.util.*, shopping.i18n.CD"  %>
<%
 Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
 if (buylist != null && (buylist.size() >0)) {
%>
   <center>
   <table border=0 cellpadding=0 width=100%
bgcolor='<%=session.getValue("cart.bgcolor")%>'>
   <tr>
        <td><%=session.getValue("cd.albumLabel")%></td>
        <td><%=session.getValue("cd.artistLabel")%></td>
        <td><%=session.getValue("cd.countryLabel")%></td>
        <td><%=session.getValue("cd.priceLabel")%></td>
      <td><%=session.getValue("cd.quantityLabel")%></td>
        <td></td>
  </tr>
  <%
     for (int index=0; index< buylist.size();index++) {
        CD anOrder = (CD) buylist.elementAt(index);
  %>
         <tr>
                <td><%=anOrder.getAlbum()%></td>
                <td><%=anOrder.getArtist()%></td>
                <td><%=anOrder.getCountry()%></td>
                <td><%=anOrder.getPrice()%></td>
                <td><%=anOrder.getQuantity()%></td>
                <td>
                <form name="deleteForm" action="main.jsp" method="post">
                  <input type="submit"
                  value='<%=session.getValue("cart.delLabel")%>'>
                    <input type="hidden" name=delindex value='<%=index%>'>
                    <input type="hidden" name="action" value="DELETE">
                 </form>
                </td>
        </tr>
  <% } %>
   </table>
   <p>
   <form name="checkoutForm"  action="main.jsp"  method="post">
     <input type="hidden" name="action" value="CHECKOUT">
     <input type="submit" name="Checkout"
  value='<%=session.getValue("cart.checkoutLabel")%>'>
   </form>
   </center>
<% } %>


If the shopping cart is not empty, the selected CDs are extracted from the cart one at a time, as demonstrated by the following scriptlet:

<%
  for (int index=0; index < buylist.size(); index++) {
    CD anOrder = (CD) buylist.elementAt(index);
%>


Once the variable describing an item has been created, it is simply inserted into the static HTML template using JSP expressions. You can avoid having an excessive amount of script code when iterating beans with indexed properties by implementing the looping via custom tags. Figure 3 shows the Swedish view after the user has placed some items into the shopping cart. Contrast it with the English view for the same procedure, as shown in Figure 4. Observe how seamlessly the localization is performed while using the same underlying application logic.

Figure 3. Shopping cart view localized for Swedish

Figure 4. Shopping cart view localized for English

  • Print
  • Feedback