• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shopping cart servlets

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
nelson christos
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic