Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 2 of 3
| Listing 4: CD.java |
|---|
package shopping;
public class CD {
String album;
String artist;
String country;
float price;
int quantity;
public CD() {
album="";
artist="";
country="";
price=0;
quantity=0;
}
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(float p) {
price=p;
}
public float getPrice() {
return price;
}
public void setQuantity(int q) {
quantity=q;
}
public int getQuantity() {
return quantity;
}
}
|
Notice that we have also included additional intelligence within the servlet, so that it understands that, if a previously
added CD is reselected, it should simply increase the count for that CD bean within the shopping cart. The controller servlet
also processes actions triggered from within Cart.jsp, such as the user deleting items from the shopping cart, or proceeding to the checkout counter. Observe that the controller
always has complete control over which resources should be invoked in response to specific actions. For example, changes made
to the state of the shopping cart, such as additions or deletions, cause the controller servlet to forward the request after
processing to the Eshop.jsp page. This in turn causes the page to redisplay 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 the Checkout.jsp page (shown in Listing 5) by means of the following request dispatcher, as shown below:
String url="/jsp/shopping/Checkout.jsp"; ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); rd.forward(req,res);
| Listing 5: Checkout.jsp |
|---|
<%@ page session="true" import="java.util.*, shopping.CD" %>
<html>
<head>
<title>Music Without Borders Checkout</title>
</head>
<body bgcolor="#33CCFF">
<font face="Times New Roman,Times" size=+3>
Music Without Borders Checkout
</font>
<hr><p>
<center>
<table border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>
<td><b>ALBUM</b></td>
<td><b>ARTIST</b></td>
<td><b>COUNTRY</b></td>
<td><b>PRICE</b></td>
<td><b>QUANTITY</b></td>
<td></td>
</tr>
<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
String amount = (String) request.getAttribute("amount");
for (int i=0; i < buylist.size();i++) {
CD anOrder = (CD) buylist.elementAt(i);
%>
<tr>
<td><b><%= anOrder.getAlbum() %></b></td>
<td><b><%= anOrder.getArtist() %></b></td>
<td><b><%= anOrder.getCountry() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
</tr>
<%
}
session.invalidate();
%>
<tr>
<td> </td>
<td> </td>
<td><b>TOTAL</b></td>
<td><b>$<%= amount %></b></td>
<td> </td>
</tr>
</table>
<p>
<a href="/examples/jsp/shopping/EShop.jsp">Shop some more!</a>
</center>
</body>
</html>
|
Checkout.jsp 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. Once the user goes to the checkout counter, it is equally
important to get rid of the session object. That is taken care of by having a session.invalidate() invocation at the end of the page. This process is necessary for two reasons. First, if the session is not invalidated, the
user's shopping cart is not reinitialized; if the user then attempts to commence another round of shopping upon checkout,
her shopping cart will continue to hold items that she has already purchased. The second reason is that if the user simply
left the site upon checkout, the session object will not be garbage collected and will continue to take up valuable system
resources until its lease period expires. Since the default session-lease period is about 30 minutes, this can quickly lead
to the system running out of memory in a high-volume system. Of course, we all know what happens to an application that runs
out of system resources!