This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes My Shopping cart does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "My Shopping cart does not work" Watch "My Shopping cart does not work" New topic
Author

My Shopping cart does not work

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
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 :



SCJP 6.0 (86%) SCWCD 5 (94%)
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263

Because this will return null I think.


SCJP 1.5(97%) My Blog
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
yes. How am i going to fix it?
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Add following in index html
Enter Item name <input type="text" name="title" />
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
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
in jsp
<input type="submit" id="Perfume" name="command" value="Perfume">

and in servlet


saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
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

Joined: Jan 14, 2010
Posts: 317

Change your code to

shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
<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

Joined: Oct 05, 2009
Posts: 148
@ 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
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
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
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

Joined: Oct 05, 2009
Posts: 148
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
Welcome! It's good to know that my suggestions worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My Shopping cart does not work
 
Similar Threads
Newbie: Need help with checkbox post parameters
How To Convert a Servlet File to a JSP File
shopping cart prob
passing hidden forms problem
Why doesn't exercise 8-1 from Murach's Java Servlets work for me on IE7?