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 5 of 6

The important thing to observe here is that the processing for all actions carried out within either eshop.jsp or cart.jsp is handled by the controller JSP page, main.jsp, shown in Listing 6.

Listing 6: main.jsp

<%@ page import="java.util.*, java.text.*, shopping.i18n.CD" %>
<%
  String action = request.getParameter("action");
  if (action == null) {
%>
    <jsp:forward page="i18nDemo.jsp" />
<%
  } else  {
    Vector buylist=
              (Vector)session.getValue("shopping.shoppingcart");
    if (!action.equals("CHECKOUT")) {
        if (action.equals("DELETE")) {
          String del = request.getParameter("delindex");
          int delIndex = (new Integer(del)).intValue();
          buylist.removeElementAt(delIndex);
        } else if (action.equals("ADD")) {
            //any previous buys of same cd?
          boolean match=false;
%>
        <jsp:useBean id="aCD" class="shopping.i18n.CD" scope="page">
             <% aCD.setCDProperties(request); %>
        </jsp:useBean>
<%
          if (buylist==null) {
            //add first cd to the cart
            buylist = new Vector(); //first order
            buylist.addElement(aCD);
       } else { // not first buy
            for (int i=0; i< buylist.size();i++) {
              CD cd = (CD) buylist.elementAt(i);
                if (cd.getAlbum().equals(aCD.getAlbum())) {
                   int tmpQty =
                  (new Integer(cd.getQuantity())).intValue() +
                        (new Integer(aCD.getQuantity())).intValue();
                   cd.setQuantity(new Integer(tmpQty).toString());
                   buylist.setElementAt(cd,i);
                   match = true;
              } //end of if name matches
            } // end of for
            if (!match) buylist.addElement(aCD);
         } //end of not first buy
} //end of action==ADD
        session.putValue("shopping.shoppingcart", buylist);
%>
        <jsp:forward page="eshop.jsp" />
<%
} else { // if checkout
            NumberFormat nFormat = NumberFormat.getCurrencyInstance(
                                           (Locale)session.getValue("myLocale"));
            float total =0;
            for (int i=0; i< buylist.size();i++) {
              CD anOrder = (CD) buylist.elementAt(i);
                Number n = nFormat.parse(anOrder.getPrice().trim());
              float price= n.floatValue();
              int qty = (new Integer(anOrder.getQuantity())).intValue();
              total += (price * qty);
           }
           String amountDue=nFormat.format(total);
           request.setAttribute("amountDue",amountDue);
%>
           <jsp:forward page="checkout.jsp" />
<%
        }
}
%>


If the user tries to add or delete an item, or checks out, the request is posted to the controller, main.jsp. This controller page handles addition requests initiated from eshop.jsp, as well as deletion and checkout requests triggered from cart.jsp. If the request is an addition, for instance, the controller processes the request parameters for the item to be added, and then instantiates a new CD bean (shown in Listing 7) representing the selection. The updated shopping-cart object is then placed back within the session. The controller is also enabled with enough smarts to understand that if a previously added CD is reselected, the controller should simply increase the count for that CD bean within the shopping cart. Changes affecting the state of the shopping cart, such as an addition or deletion, cause the controller to forward the request after processing to eshop.jsp. This, in turn, redisplays the main view, along with the updated contents of the shopping cart. If the user decides to check out, the request is forwarded after processing to checkout.jsp, shown in Listing 8.

A big advantage of having a separate controller page such as this one is that you can always exercise complete control over the resources that should be invoked in response to specific actions. In fact, you can further isolate this by having the controller initialize itself via a special property file containing target resources for specific user actions. That way, you can completely externalize the maintenance of your Web application and gain maximum flexibility.

Listing 7: CD.java

package shopping.i18n;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class CD {
        String album;
        String artist;
        String country;
        String price;
        String quantity;
        public CD() {
                album="";
                artist="";
                country="";
                price="";
                quantity="";
        }
        public void setCDProperties(HttpServletRequest req) {
                String myCd = req.getParameter("CD");
                StringTokenizer t = new StringTokenizer(myCd,"|");
                this.album= t.nextToken();
                this.artist = t.nextToken();
                this.country = t.nextToken();
                this.price = t.nextToken().trim();
                this.quantity = req.getParameter("qty");
        }
        public void setAlbum(String title) {
                album=title;
        }
        public String getAlbum() {
                return album;
        }
        public void setArtist(String group) {
                artist=group;
        }
        public String getArtist() {
                return artist;
        }
        public void setCountry(String cty) {
                country=cty;
        }
        public String getCountry() {
                return country;
        }
        public void setPrice(String p) {
                price=p;
        }
        public String getPrice() {
                return price;
        }
        public void setQuantity(String q) {
                quantity=q;
        }
        public String getQuantity() {
                return quantity;
        }
 }


The checkout.jsp page simply extracts the shopping cart from the session and the total amount for the request, and then displays the selected items and their total cost. Figure 5 shows the client view upon checkout, localized for German.

Figure 5. Checkout view localized for German

  • Print
  • Feedback