Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My Shopping cart does not work

 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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 :

 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because this will return null I think.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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 ?
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your code to

 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Welcome! It's good to know that my suggestions worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic