Understanding "music without borders"
The main interface of "music without borders" is Eshop. JSP on the jsp page (see Code List 1 ). You will notice that this page serves almost only as a dedicated user interface and does not undertake any processing tasks-it is an ideal JSP solution. In addition, pay attention to the other JSP page Cart. jsp (see Code List 2) is Eshop. jsp commands <jsp: include page = "Cart. jsp "flush =" true "/> is included in it.
Code List 1: EShop. jsp
<% @ Page session = "true" %>
<Html>
<Head>
<Title> Music Without Borders </title>
</Head>
<Body bgcolor = "#33 CCFF">
<Font face = "Times New Roman, Times" size = "+ 3">
Music Without Borders
</Font>
<Hr> <p>
<Center>
<Form name = "shoppingForm"
Action = "/examples/servlet/ShoppingServlet"
Method = "POST">
<B> CD: </B>
<Select name = CD>
<Option> Yuan | The Guo Brothers | China | $14.95 </option>
<Option> Drums of Passion | Babatunde Olatunji | Nigeria | $16.95 </option>
<Option> Kaira | Tounami Diabate | Mali | $16.95 </option>
<Option> The Lion is Loose | Eliades Ochoa | Cuba |$ 13.95 </option>
<Option> Dance the dedevil Away | Outback | Australia | $14.95 </option>
<Option> Record of Changes | Samulnori | Korea | $12.95 </option>
<Option> Djelika | Tounami Diabate | Mali | $14.95 </option>
<Option> Rapture | Nusrat Fateh Ali Khan | Pakistan | $12.95 </option>
<Option> Cesaria Evora | Cape Verde | $16.95 </option>
<Option> Ibuki | Kodo | Japan | $13.95 </option>
</Select>
<B> Quantity: </B> <input type = "text" name = "qty" SIZE = "3" value = 1>
<Input type = "hidden" name = "action" value = "ADD">
<Input type = "submit" name = "Submit" value = "Add to Cart">
</Form>
</Center>
<P>
<Jsp: include page = "Cart. jsp" flush = "true"/>
</Body>
</Html>
Code List 2: Cart. jsp
<% @ Page session = "true" import = "java. util. *, shopping. CD" %>
<%
Vector buylist = (Vector) session. getValue ("shopping. shoppingcart ");
If (buylist! = Null & (buylist. size ()> 0 )){
%>
<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>
<%
For (int index = 0; index <buylist. size (); index ++ ){
CD anOrder = (CD) buylist. elementAt (index );
%>
<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>
<Td>
<Form name = "deleteForm"
Action = "/examples/servlet/ShoppingServlet"
Method = "POST">
<Input type = "submit" value = "Delete">
<Input type = "hidden" name = "delindex" value = '<% = index %>'>
<Input type = "hidden" name = "action" value = "DELETE">
</Form>
</Td>
</Tr>
<% }%>
</Table>
<P>
<Form name = "checkoutForm"
Action = "/examples/servlet/ShoppingServlet"
Method = "POST">
<Input type = "hidden" name = "action" value = "CHECKOUT">
<Input type = "submit" name = "Checkout" value = "Checkout">
</Form>
</Center>
<% }%>
Here, Cart. jsp controls the expression of the session-based shopping Cart. In the MVC system, the shopping Cart acts as the Model.
Observe the script snippets starting with Cart. jsp:
<%
Vector buylist = (Vector) session. getValue ("shopping. shoppingcart ");
If (buylist! = Null & (buylist. size ()> 0 )){
%>
This script mainly extracts the shopping cart from the session. If the shopping cart is empty or has not been created, nothing is displayed. Therefore, when the user visits the application for the first time, view 3 is shown to him:
Figure 3: music without borders, main view
Button text in the figure: put in the shopping cart
If the shopping cart is not empty, the selected items are retrieved from the shopping cart in sequence, as shown in the following script snippet:
<%
For (int index = 0; index <buylist. size (); index ++ ){
CD anOrder = (CD) buylist. elementAt (index );
%>
Once a variable describing an item is created, it is directly embedded into a static HTML Template Using JSP expressions. Figure 4 shows the view after the user adds some items to the shopping cart.
Figure 4: music without borders, shopping cart View
Text in the figure: Music Without Borders; Quantity: Quantity; ALBUM: recording; ARTIST: ARTIST; COUNTRY: COUNTRY; PRICE: PRICE; Delete: Delete; Checkout: Checkout.
It is important to note that in Eshop. jsp and Cart. all actions implemented in jsp are processed by a servlet-ShoppingServlet. java Control, as shown in code listing 3:
Code List 3: ShoppingServlet. java
Import java. util .*;
Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import shopping. CD;
Public class ShoppingServlet extends HttpServlet {
Public void init (ServletConfig conf) throws ServletException {
Super. init (conf );
}
Public void doPost (HttpServletRequest req, HttpServletResponse res)
Throws ServletException, IOException {
HttpSession session = req. getSession (false );
If (session = null ){
Res. sendRedirect ("http: // localhost: 8080/error.html ");
}
Vector buylist =
(Vector) session. getValue ("shopping. shoppingcart ");
String action = req. getParameter ("action ");
If (! Action. equals ("CHECKOUT ")){
If (action. equals ("DELETE ")){
String del = req. getParameter ("delindex ");
Int d = (new Integer (del). intValue ();
Buylist. removeElementAt (d );
} Else if (action. equals ("ADD ")){
// Have you purchased the same cd before?
Boolean match = false;
CD aCD = getCD (req );
If (buylist = null ){
// Put the first CD in the shopping cart
Buylist = new Vector (); // the first order
Buylist. addElement (aCD );
} Else {// not the First Purchase
For (int I = 0; I <buylist. size (); I ++ ){
CD cd = (CD) buylist. elementAt (I );
If (cd. getAlbum (). equals (aCD. getAlbum ())){
Cd. setQuantity (cd. getQuantity () + aCD. getQuantity ());
Buylist. setElementAt (cd, I );
Match = true;
} // If name matches ends
} // The for loop ends.
If (! Match)
Buylist. addElement (aCD );
}
}
Session. putValue ("shopping. shoppingcart", buylist );
String url = "/jsp/shopping/EShop. jsp ";
ServletContext SC = getServletContext ();
RequestDispatcher rd = SC. getRequestDispatcher (url );
Rd. forward (req, res );
} Else if (action. equals ("CHECKOUT ")){
Float total = 0;
For (int I = 0; I <buylist. size (); I ++ ){
CD anOrder = (CD) buylist. elementAt (I );
Float price = anOrder. getPrice ();
Int qty = anOrder. getQuantity ();
Total + = (price * qty );
}
Total + = 0.005;
String amount = new Float (total). toString ();
Int n = amount. indexOf ('.');
Amount = amount. substring (0, n + 3 );
Req. setAttribute ("amount", amount );
String url = "/jsp/shopping/Checkout. jsp ";
ServletContext SC = getServletContext ();
RequestDispatcher rd = SC. getRequestDispatcher (url );
Rd. forward (req, res );
}
}
Private CD getCD (HttpServletRequest req ){
// Imagine how ugly these are in a script snippet.
String myCd = req. getParameter ("CD ");
String qty = req. getParameter ("qty ");
StringTokenizer t = new StringTokenizer (myCd, "| ");
String album = t. nextToken ();
String artist = t. nextToken ();
String country = t. nextToken ();
String price = t. nextToken ();
Price = price. replace ('$', ''). trim ();
CD cd = new CD ();
Cd. setAlbum (album );
Cd. setArtist (artist );
Cd. setCountry (country );
Cd. setPrice (new Float (price). floatValue ());
Cd. setQuantity (new Integer (qty). intValue ());
Return cd;
}
}