MVC Model (Example)

15 February 2008

Do follow the first part of this post before going through this one.

In our example of online website for shopping books, every time the user adds a book within EShop.jsp, the request is posted to the controller servlet, ShoppingServlet.java. The servlet in turn determines the appropriate action, and then processes the request parameters for the item to be added. It then instantiates a new Book bean BOOK.java representing the selection, and goes about updating the shopping cart object before placing it back within the session.

BOOK.java

package shopping;
public class BOOK {
  String book;
  String author;
  float price;
  int quantity;
 
  public BOOK() {
    book="";
    author="";
    price=0;
    quantity=0;
  }
  public void setBook(String title) {
    book=title;
  }
  public String getBook() {
    return book;
  }
  public void setAuthor(String a) {
    author=a;
  }
  public String getAuthor() {
    return author;
  }
 public void setPrice(float p) {
    price=p;
  }
  public float getPrice() {
    return price;
  }
  public void setQuantity(int q) {
    quantity=q;
  }
  public int getQuantity() {
    return quantity;
  }
}

Checkout.jsp gets the shopping cart from the session and the total amount for the request, and then shows the selected items and their total cost.

Checkout.jsp

<%@ page session="true" import="java.util.*, shopping.BOOK" %>
<html>
<head>
<title> Shop Books Online Checkout </title>
</head>
<body >

Shop Books Online Checkout

 <hr><p>
 <center>
 <table border="0" cellpadding="0" width="100%">
 <tr>
 <td><b>BOOK</b></td>
 <td><b>AUTHOR</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++) {
   BOOK anOrder = (BOOK) buylist.elementAt(i);
 %>
 <tr>
 <td><b><%= anOrder.getBook() %></b></td>
 <td><b><%= anOrder.getAuthor() %></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>

Error page
error.html

<html>
<body>
<h1>
  Sorry, there was an error! <br>
  Please try <a href="/examples/jsp/shopping/EShop.jsp">again</a>.
</h1>
</body>
</html>

del.icio.us:MVC Model (Example)  digg:MVC Model (Example)  spurl:MVC Model (Example)  wists:MVC Model (Example)  simpy:MVC Model (Example)  newsvine:MVC Model (Example)  blinklist:MVC Model (Example)  furl:MVC Model (Example)  reddit:MVC Model (Example)  fark:MVC Model (Example)  blogmarks:MVC Model (Example)  Y!:MVC Model (Example)  smarking:MVC Model (Example)  magnolia:MVC Model (Example)  segnalo:MVC Model (Example)  gifttagging:MVC Model (Example)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: MVC Model (Example)

Leave a Reply