MVC - View Example

13 February 2008

This post is in continuation of MVC - View. Do read that before this one.

I will use an example for better understanding.

EShop.jsp is the main View, or presentation logic for our online website for shopping books. There is no processing logic within the JSP page itself. Please note that the page deals only with presenting the main user interface of the application to the client, and performs no processing whatsoever. It is just responsible for retrieving any objects or beans that may have been previously created by the servlet, and extracting the dynamic content from that servlet for insertion within static templates. Please also note that the JSP page Cart.jsp is included within EShop.jsp. Cart.jsp is used for the the presentation of the session based shopping cart. If the cart is empty or not yet created it shows nothing.

EShop.jsp

<%@ page session="true" %>
<html>
<head>
 <title>Shop Books Online</title>
</head>
<body>
 Shop Books Online
<hr><p>
 <center>
 <form name="shoppingForm" 
   action="/examples/servlet/ShoppingServlet" 
   method="POST">
 <b>BOOK:</b> 
 <select name=BOOK>
  <option>The Complete Reference Java | Herbert Schildz | 45.00</option>
  <option>Programming in C | Dennis Ritchie | 55.00</option>
  <option>SCJP | Khalid Moghul | 35.00</option>
  <option>.NET | Peter Norton | 65.00</option>
  <option>Datastructures| Tenenenbaum | 35.00</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>

Cart.jsp

<%@ page session="true" import="java.util.*, shopping.BOOK" %>
<%
 Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
 if (buylist != null && (buylist.size() > 0)) {
%>
<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>
 <%
  for (int index=0; index < buylist.size();index++) {
   BOOK anOrder = (BOOK) buylist.elementAt(index);
 %>
 <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>
  <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>
<% } %>

del.icio.us:MVC - View Example  digg:MVC - View Example  spurl:MVC - View Example  wists:MVC - View Example  simpy:MVC - View Example  newsvine:MVC - View Example  blinklist:MVC - View Example  furl:MVC - View Example  reddit:MVC - View Example  fark:MVC - View Example  blogmarks:MVC - View Example  Y!:MVC - View Example  smarking:MVC - View Example  magnolia:MVC - View Example  segnalo:MVC - View Example  gifttagging:MVC - View 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 - View Example

Leave a Reply