Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

I know what to do but don't know how to implement it..

Ranch Hand
Posts: 644
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am developing a small shopping cart application in which I have one servlet. I am displaying the total items in the shopping cart. I have provided one button to proceed to checkout. At this stage if the user wants to update the number of items (he wants to order)he can do that by just changing the no of items text field. I know what to do when the user cliks the "update" button but I don't know exactly where to place the code. I mean there are 2 buttons so how to tell the servlet what to do when a particular button is pressed.
When the user cliks the "update" button the displaying items going to be same (same page is going to be displayed)only the ordered quantity (field) is going to change.

below is the code from the servlet.
Vector itemsOrdered = cart.getItemsOrdered();
if(itemsOrdered.size() == 0)
out.println("<h2><i>No items in your cart...</i></h2>");
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;
NumberFormat formatter = NumberFormat.getCurrencyInstance();

String formURL = "servlet/myServlet.OrderPage";
formURL = response.encodeURL(formURL);

for(int i=0;i<itemsOrdered.size();i++)
order= (ItemOrder)itemsOrdered.elementAt(i);
"<td>"+ order.getItemId()+"\n"+
"<td>"+ order.getShortDescription()+"\n"+
"<form action=\""+formURL+"\">\n"+
"<input type=\"hidden\" name=\"itemId\" "+
"value = \""+order.getItemId()+"\">\n"+
"<input type=\"text\" name=\"numItems\" "+
"size=13 value = \""+
"<input type=\"submit\"\n "+
"value = \"Update Order\">\n"+
formatter.format(order.getTotalCost()) );


String checkOutURL = response.encodeURL("/myDevelopment/jsp/CheckOut.jsp");
"<form action=\""+checkOutURL+"\">\n"+
"<input type=\"Submit\" \n "+
"value = \"proceed to checkout\">\n"+

} // synchronized

author & internet detective
Posts: 40912
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have the form's action send you to a different servlet depending on which button they clicked (using a different URL.) Alternatively, you can use a hidden input form field. You set this field to have a different value in each form. Then you can check the value of this parameter in your servlet.
    Bookmark Topic Watch Topic
  • New Topic