i am doing a project on a shopping mall and i had to implement the shopping cart concept.On a item description page there is a Addtocart button which goes into a servlet adds the item in the session table and i was generating a html page on the fly giving details of that item in the a tabular form which has a Removeitem button on the last column of each row so if there are suppose 3 items purchased each item will have its own Removeitem button on its last column. if this Removeitem button is pressed a another servlet gets invoked to remove the item. My problem is that how do i known which rows Removeitem has been pressed so that i can remove that item from the session table i am using hidden field for session tracking please help in detail if possible give a simple prg showing this thankyou
Dear Anis: When u r setting up the RemoveItem button in the form u need to give different value for each button like this <input type="submit" name ="remove" value ="item1"> <input type="submit" name ="remove" value ="item2"> <input type="submit" name ="remove" value ="item3"> then in ur servlet u can get the value and check like this String selectedItem = request.getParameter("remove"); if( selectedItem.equals("item1")){ .... else if(selectedItem.equals("item2")){ ...... I hope u r getting the idea. Anyway let me know whether this helped u r not okay rgds Siva
Hi! I feel it is fairly simple. When the user want to verify his cart, use a servlet, say ServletA, to show the items selected with an option to remove the item not needed (u can use proper html input type like checkbox,radio buttion etc. as desired/required). This servlet should call another servlet, say ServletB. ServletB should parse the parameters passed to it and delete them in database using the session identification(through JDBC) after getting a final confirmation from the user. I think this u can do yourself. I am sure you will find free sites in internet for servlet where you can find source codes. Regards, K.Subramanian
anis mistry
Ranch Hand
Joined: Nov 11, 2000
Posts: 52
posted
0
dear mr siva i saw your reply and it was helpful but buttons value should be Remove on every row and not item1,item2...... and so on rgrds anis [This message has been edited by anis mistry (edited November 27, 2000).]
Siva Jagadeesan
Ranch Hand
Joined: Oct 31, 2000
Posts: 160
posted
0
Hi Anis : Okay now i understand what u need. Infact if u want to identify the button then it SHOULD have different value like item1 item2 etc. I saw an example in CoreSerlets book exactly like this package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.text.NumberFormat; /** Shows all items currently in ShoppingCart. Clients * have their own session that keeps track of which * ShoppingCart is theirs. If this is their first visit * to the order page, a new shopping cart is created. * Usually, people come to this page by way of a page * showing catalog entries, so this page adds an additional * item to the shopping cart. But users can also * bookmark this page, access it from their history list, * or be sent back to it by clicking on the "Update Order" * button after changing the number of items ordered. * <P> * Taken from Core Servlets and JavaServer Pages * from Prentice Hall and Sun Microsystems Press, * http://www.coreservlets.com/. * � 2000 Marty Hall; may be freely used or adapted. */ public class OrderPage extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); ShoppingCart cart; synchronized(session) { cart = (ShoppingCart)session.getValue("shoppingCart"); // New visitors get a fresh shopping cart. // Previous visitors keep using their existing cart. if (cart == null) { cart = new ShoppingCart(); session.putValue("shoppingCart", cart); } String itemID = request.getParameter("itemID"); if (itemID != null) { String numItemsString = request.getParameter("numItems"); if (numItemsString == null) { // If request specified an ID but no number, // then customers came here via an "Add Item to Cart" // button on a catalog page. cart.addItem(itemID); } else { // If request specified an ID and number, then // customers came here via an "Update Order" button // after changing the number of items in order. // Note that specifying a number of 0 results // in item being deleted from cart. int numItems; try { numItems = Integer.parseInt(numItemsString); } catch(NumberFormatException nfe) { numItems = 1; } cart.setNumOrdered(itemID, numItems); } } } // Whether or not the customer changed the order, show // order status. response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Status of Your Order"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>"); synchronized(session) { Vector itemsOrdered = cart.getItemsOrdered(); if (itemsOrdered.size() == 0) { out.println("<H2><I>No items in your cart...</I></H2>"); } else { // If there is at least one item in cart, show table // of items ordered. out.println ("<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Item ID<TH>Description\n" + " <TH>Unit Cost<TH>Number<TH>Total Cost"); ItemOrder order;
// Rounds to two decimal places, inserts dollar // sign (or other currency symbol), etc., as // appropriate in current Locale. NumberFormat formatter = NumberFormat.getCurrencyInstance(); String formURL = "/servlet/coreservlets.OrderPage"; // Pass URLs that reference own site through encodeURL. formURL = response.encodeURL(formURL);
// For each entry in shopping cart, make // table row showing ID, description, per-item // cost, number ordered, and total cost. // Put number ordered in textfield that user // can change, with "Update Order" button next // to it, which resubmits to this same page // but specifying a different number of items. for(int i=0; i<itemsOrdered.size(); i++) { order = (ItemOrder)itemsOrdered.elementAt(i); out.println ("<TR>\n" + " <TD>" + order.getItemID() + "\n" + " <TD>" + order.getShortDescription() + "\n" + " <TD>" + formatter.format(order.getUnitCost()) + "\n" + " <TD>" + "<FORM ACTION=\"" + formURL + "\">\n" + "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\"\n" + " VALUE=\"" + order.getItemID() + "\">\n" + "<INPUT TYPE=\"TEXT\" NAME=\"numItems\"\n" + " SIZE=3 VALUE=\"" + order.getNumItems() + "\">\n" + "<SMALL>\n" + "<INPUT TYPE=\"SUBMIT\"\n "+ " VALUE=\"Update Order\">\n" + "</SMALL>\n" + "</FORM>\n" + " <TD>" + formatter.format(order.getTotalCost())); } String checkoutURL = response.encodeURL("/Checkout.html"); // "Proceed to Checkout" button below table out.println ("</TABLE>\n" + "<FORM ACTION=\"" + checkoutURL + "\">\n" + "<BIG><CENTER>\n" + "<INPUT TYPE=\"SUBMIT\"\n" + " VALUE=\"Proceed to Checkout\">\n" + "</CENTER></BIG></FORM>"); } out.println("</BODY></HTML>"); } } /** POST and GET requests handled identically. */
protected void setItems(String[] itemIDs) { this.itemIDs = itemIDs; items = new Item[itemIDs.length]; for(int i=0; i<items.length; i++) { items[i] = Catalog.getItem(itemIDs[i]); } } /** Sets the page title, which is displayed in * an H1 heading in resultant page. * <P> * Servlets that extend CatalogPage <B>must</B> call * this method (usually from init) before the servlet * is accessed. */
protected void setTitle(String title) { this.title = title; } /** First display title, then, for each catalog item, * put its short description in a level-two (H2) heading * with the price in parentheses and long description * below. Below each entry, put an order button * that submits info to the OrderPage servlet for * the associated catalog entry. * <P> * To see the HTML that results from this method, do * "View Source" on KidsBooksPage or TechBooksPage, two * concrete classes that extend this abstract class. */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); if (items == null) { response.sendError(response.SC_NOT_FOUND, "Missing Items."); return; } PrintWriter out = response.getWriter(); out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>"); Item item; for(int i=0; i<items.length; i++) { out.println("<HR>"); item = items[i]; // Show error message if subclass lists item ID // that's not in the catalog. if (item == null) { out.println("<FONT COLOR=\"RED\">" + "Unknown item ID " + itemIDs[i] + "</FONT>"); } else { out.println(); String formURL = "/servlet/coreservlets.OrderPage"; // Pass URLs that reference own site through encodeURL. formURL = response.encodeURL(formURL); out.println ("<FORM ACTION=\"" + formURL + "\">\n" + "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\" " + " VALUE=\"" + item.getItemID() + "\">\n" + "<H2>" + item.getShortDescription() + " ($" + item.getCost() + ")</H2>\n" + item.getLongDescription() + "\n" + "<P>\n<CENTER>\n" + "<INPUT TYPE=\"SUBMIT\" " + "VALUE=\"Add to Shopping Cart\">\n" + "</CENTER>\n<P>\n</FORM>"); } } out.println("<HR>\n</BODY></HTML>"); } /** POST and GET requests handled identically. */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } Hope this helps u rgds Siva [This message has been edited by Sivakumar Jagadeesan (edited November 27, 2000).]
Hi! My full name is Subramanian Kalyanasundaram, where Kalyanasundaram is my father's name. My name is K.Subramanian every where. If i replace the full name of Subramanian Kalyanasundaram it will exceed the width of UserName. So, I preferred this name. Please let me know whether I have to change my name to follow the rule. K.Subramanian