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 :
This message was edited 1 time. Last update was at by Ulf Dittmer
Add following in index html
Enter Item name <input type="text" name="title" />
saima kanwal
Ranch Hand
Joined: Oct 05, 2009
Posts: 148
posted
0
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
Joined: Aug 26, 2008
Posts: 263
posted
0
in jsp
<input type="submit" id="Perfume" name="command" value="Perfume">
and in servlet
This message was edited 1 time. Last update was at by shivendra tripathi
saima kanwal
Ranch Hand
Joined: Oct 05, 2009
Posts: 148
posted
0
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 ?
Try above with your original servlet code. I guess it should work.
saima kanwal
Ranch Hand
Joined: Oct 05, 2009
Posts: 148
posted
0
@ 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
Joined: Aug 26, 2008
Posts: 263
posted
0
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
Joined: Oct 05, 2009
Posts: 148
posted
0
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
Joined: Aug 26, 2008
Posts: 263
posted
0
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.
This message was edited 1 time. Last update was at by shivendra tripathi
saima kanwal
Ranch Hand
Joined: Oct 05, 2009
Posts: 148
posted
0
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
Joined: Aug 26, 2008
Posts: 263
posted
0
Welcome! It's good to know that my suggestions worked