File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes shopping cart servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "shopping cart servlets" Watch "shopping cart servlets" New topic
Author

shopping cart servlets

Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Hi,

I am not very experienced in servlets and am working on making a simple shopping cart. This application has 3 servlets and one of these servlets (ReviewShoppingCart) has a form which has 2 submit buttons. One button should point to AddToShoppingCart servlet and the other should point to RemoveFromCart.

But I don't really know how to have the if/else code for that. I mean I tried the code and it is pasted below.



Please help me with this. I would be extremely grateful.

Ricky
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
hi ricky

you have to use javascript and set the a parameter to the required action
eg document.formName.parameterName.value="ADD" for add button click
and in your servlet get the paramter value and forward it to the appropriate servlet


i think therefore i am
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by nelson christos:
hi ricky

you have to use javascript and set the a parameter to the required action
eg document.formName.parameterName.value="ADD" for add button click
and in your servlet get the paramter value and forward it to the appropriate servlet


Thanks for the reply Nelson.
But can you please elaborate a bit because I am not very good in JavaScript. It would help immensely if you could paste a sample code for that.
Ricky
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

While Javascript could be used on the original page, it has nothing to do with what happens after submission. I assume that the code you showed in the first post is in the servelt that you are submitting to? Please elaborate.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
hi ricky

the button needs to be something like this
input type="button" value="Add" onclickButton ="javascript:submitForm('ADD')"

and in the heaD tag of your html do this
< script
function submitForm(ac)
{
document.formName.action.value=ac;
document.formName.submit();
}
< script
and in your servlet get the paramter value and forward it to the appropriate servlet

String actionString = request.getParameter("action");

if (actionString.equals("ADD")) {
AddToShoppingCart(request, aPW);
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

It looks like he's already successfully getting a value named "actionString" to his servlet, so all that Javascript doesn't seem neccessary. I'm not even sure what the problem really is.

So, what is the problem?
[ April 03, 2007: Message edited by: Bear Bibeault ]
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
i think in the servlet he is getting the value of "actionString"
but that value remains same for both the buttons "Add" and "Delete"
so the js might help him. lets ask him what the problem is
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by Bear Bibeault:
It looks like he's already successfully getting a value named "actionString" to his servlet, so all that Javascript doesn't seem neccessary. I'm not even sure what the problem really is.

So, what is the problem?

[ April 03, 2007: Message edited by: Bear Bibeault ]


Thanks for the replies Bear and Nelson.

I am out of town for a few days and when I get back home to my machine, I'll paset the entire code and explanation of the problem.

Thanks again for helping me with this.

Cheers
Ricky
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Hello Bear and Nelson,

I am new to Servlets so this problem migth be too basic for you but at the moment it IS a problem for me!

I am working on a simple shopping cart and have 3 servlets (I have pasted the code below):
1. AddToShoppingCart (has 1 button 'View Items')
2. ReviewShoppingCart (has 2 buttons, 'Add to cart' and 'Remove Item')
3. RemoveItemsFromCart (has 2 buttons, 'Add to cart' and 'Remove Item')

When I add items to the cart, the items get added and show with a checkbox against it(which is selected). I want to program this application so that when I de-select the checkboxes against the items, and hit 'Remove Items', these items get removed from the page. At the moment when I deselect the items and hit 'remove', they remain on the screen and come back 'selected'.

I have used Hidden Forms for Session Tracking. The problem I posted earlier was because I thought that since I am using 2 forms on 1 page, that might be causing a problem so I wanted a method to use 1 form on the page with a way to call the required servlet so that I could bypass the problem of having the hidden forms twice on the page.

I would be really thankful if you could point me in the right direction.

Thanks in advance.
Ricky






[ April 09, 2007: Message edited by: Ricky James ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: shopping cart servlets