MVC Controller (example)
14 February 2008This post contains an example for MVC controller. Do read “MVC controller” post before this one.
Every time the user adds a book using EShop.jsp, the request is posted to the controller servlet ShoppingServlet.java, for example the user deleting books from the shopping cart, or proceeding to the checkout counter etc. Then servlet takes an appropriate action, and processes the request parameters for the item to be added. It then instantiates a new BOOK bean representing the selection, and goes about updating the shopping cart object before placing it back within the session.
ShoppingServlet.java
import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import shopping.BOOK; 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")) { //any previous buys of same book? boolean match=false; BOOK aBOOK = getBOOK(req); if (buylist==null) { //add first book to the cart buylist = new Vector(); //first order buylist.addElement(aBOOK); } else { // not first buy for (int i=0; i< buylist.size(); i++) { BOOK book = (BOOK) buylist.elementAt(i); if (book.getBook().equals(aBOOK.getBook())) { book.setQuantity(book.getQuantity()+aBOOK.getQuantity()); buylist.setElementAt(book,i); match = true; } //end of if name matches } // end of for if (!match) buylist.addElement(aBOOK); } } 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++) { BOOK anOrder = (BOOK) 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 BOOK getBOOK(HttpServletRequest req) { String myBook = req.getParameter("BOOK"); String qty = req.getParameter("qty"); StringTokenizer t = new StringTokenizer(myBook,"|"); String author = t.nextToken(); String price = t.nextToken(); price = price.replace('$',' ').trim(); BOOK book = new BOOK(); book.setBook(book); book.setAuthor(author); book.setPrice((new Float(price)).floatValue()); book.setQuantity((new Integer(qty)).intValue()); return book; } }
Related Posts:
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 Controller (example)