This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

My Shopping cart does not work

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am currently learning the topic of "sessions" for SCWCD. I tried to make a simple shopping cart web app that is not complete yet. Up till now I have amde a shopping cart that just adds an item when a submit button is clicked (i have not coded the remove button yet). I am able to add the item , bbut my servlet is not reading the id or title of the item , it just adds "null" whenever any of the three buttons are clicked. Can anyone tell me where am i making a mistake??

index.html file:


result.jsp file is :




MyShoppingCartServlet.java is :

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Because this will return null I think.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. How am i going to fix it?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add following in index html
Enter Item name <input type="text" name="title" />
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then there will be text box and we have to enter the item name in it. But this is not the way shopping carts have , i mean they do not have text boxes for items. Can you please tell me a way that when i click the submit button i pass an extra parameter of item (for perfume,...) along with it ?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in jsp
<input type="submit" id="Perfume" name="command" value="Perfume">

and in servlet


 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok , that works but there is one problem : the submit button has the value "Perfume " written on it ( which is not what happens normally. when we see a shopping cart anywhere on a website. we have "add to cart" written on the button.). Is there any way that the submit button displays "add to cart" and we pass another parameter associated with it having the item name , like "perfume"? how can we do that ?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change your code to

 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<input type="submit" id="Perfume" name="command" value="Add to cart"> <input type="hidden" name="title" value="Perfume"/> </input>

Try above with your original servlet code. I guess it should work.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Shivendra Tripathi: Thanks for your reply. It helped but it is adding perfume all the time no matter which ever button i click on. I am posting my modified code.I do not know why it does it.

My servlet code is:



Index.html is:

result.jsp code is :
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome!
Revert your servlet code back to as suggested by me and try this in your JSP.


I haven't tried it myself. But hope it should work.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not working .. Using your servlet code, it just adds one value to command array and it is "add to cart" but perfume is still not added.any other suggestions???
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have an empty hidden variable and populate the value onClick in java script function.



Note: this is pseudo code. You may have to make some changes but concept should work.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks shivendra tripathi!! your efforts really helped me , and finally i got what i wanted!! witha little change in just the index.html & result.jsp ( but the concept was the same as you suggested!!) and keeping the same servlet code as yours , i got my shopping cart adding items correctly.

index.html code is :



result.jsp code is :



Thanks shivenrda , once again.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome! It's good to know that my suggestions worked
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic