wood burning stoves 2.0*
The moose likes Servlets and the fly likes I know what to do but don't know how to implement it.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "I know what to do but don Watch "I know what to do but don New topic
Author

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

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hi all,
I am developing a small shopping cart application in which I have one orderPage.java 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 orderPage.java servlet.
synchronized(session)
{
Vector itemsOrdered = cart.getItemsOrdered();
if(itemsOrdered.size() == 0)
{
out.println("<h2><i>No items in your cart...</i></h2>");
}//if
else
{
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);
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\" "+
"value = \""+order.getItemId()+"\">\n"+
"<input type=\"text\" name=\"numItems\" "+
"size=13 value = \""+
order.getNumItems()+"\">\n"+
"<small>\n"+
"<input type=\"submit\"\n "+
"value = \"Update Order\">\n"+
"</small>\n<p>\n</form>"+
"<td>"+
formatter.format(order.getTotalCost()) );



}//for

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

} // synchronized

thanks,
trupti
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30146
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I know what to do but don't know how to implement it..
 
Similar Threads
How To Convert a Servlet File to a JSP File
Storing of Data
session tracking remove item (urgent)
passing hidden forms problem
A href command not working