*
The moose likes JSP and the fly likes Shopping cart help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Shopping cart help" Watch "Shopping cart help" New topic
Author

Shopping cart help

Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
I'm currently working on a school project that deals with shopping cart. I'm not using MVC 2 Model, Cos I don't learn struts and so things are getting complicated.
I couldn't seem to add any other items into the cart, other than the ID and the quantity. What should I do if I want to add the rest of the string from the data, i.e. unit Price and total price?

Here's the following codes in mind (will be quite long):

Or you can PM me if you want the whole set of files.

Thanks & regards.

This is the product JSP page:

The problem lies with this page:

The Java files:



DAO:



Data:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well I think you need to provide one more code and that is of the action which is handling the form of the first page. In the first page you have written the action of your form as dispatcher. You code for listEmp2.jsp is also wrong I think. There you have used an object order in line 48. But I can't find that object declared in the file. Also in line 74 you are using the object item but you have commented that object on line 97.

The solution to your problem is simple. In the dispatcher action you would be adding objects of LineItemsData class to the session. You have not included that file too. But I don't think that file is needed. When you receive a request to add a medicine to the shopping cart, just take the ID of that medicine and get that object from the database. Then add the whole object into the session. So then in the JSP page you'll not have to do this

LineItemsData lItem = (LineItemsData) e.nextElement();
MedicinesData med = accessBean.findByPrimaryKey(lItem.nProductID);

Instead you will be able to do this

MedicinesData med = (MedicinesData) e.nextElement();

Also I would suggest that you should not use scripting in JSP pages...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
Ankit Garg wrote:Well I think you need to provide one more code and that is of the action which is handling the form of the first page. In the first page you have written the action of your form as dispatcher. You code for listEmp2.jsp is also wrong I think. There you have used an object order in line 48. But I can't find that object declared in the file. Also in line 74 you are using the object item but you have commented that object on line 97.


Hmms, which first page are you referring to? If you're referring to the first page, as in the previous page before that, then it's just:

which I felt is quite redundant to post. I kind of follow the script from JSPCart http://www.neurospeech.com/Products/JspCart.html And it has this:

Again, it looks quite similar to the one that I'm doing.

But because I use hidden values:


So it seems like the vector can only load quality and the Medicine ID into the next page.
We were suggested to actually use setAttributes() to bring the data over. How do I go about doing it?
Many thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I am talking about this thing

<FORM ACTION="dispatcher" METHOD="POST">

There must be a servlet mapped to dispatcher URL, just post the code of that. If you can't, then it's simple to do if you understand my instructions. Your page will give you the quantity and ID of the medicine. You can take that to get the medicine that the user chose and add it to the session as an attribute. To do this I would recommend that you build a List and add it as an attribute to the session. Whenever a new item is added to the cart, just take out the list from the session and update it. I think you'll understand what I am trying to say...
Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
Ankit Garg wrote:I am talking about this thing

<FORM ACTION="dispatcher" METHOD="POST">

There must be a servlet mapped to dispatcher URL, just post the code of that. If you can't, then it's simple to do if you understand my instructions. Your page will give you the quantity and ID of the medicine. You can take that to get the medicine that the user chose and add it to the session as an attribute. To do this I would recommend that you build a List and add it as an attribute to the session. Whenever a new item is added to the cart, just take out the list from the session and update it. I think you'll understand what I am trying to say...


I see. I'm not sure if this dispatcher servlet affects anything because it's a "universal" servlet that must be used to forward any JSP page.
It's a "rule" from school, we cannot post directly to a JSP page.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jia Bin Tang wrote: I'm not using MVC 2 Model


There is no such thing called MVC2 . Model2 design is nothing but MVC
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Ok now I got your code. But still I don't understand what you basically want. Since there is no servlet between your JSPs (there is one but it is just a dummy), so there is no way for you to find the total price. You can get the per unit price though in your jsp like this



And you can also calculate the total price based on quantity and unit price. The code might be complicated to understand at first



the javascript method would look like this



I have not tied the code so it might fail...
Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
seetharaman venkatasamy wrote:
Jia Bin Tang wrote: I'm not using MVC 2 Model


There is no such thing called MVC2 . Model2 design is nothing but MVC


Haha I see, I always thought it's something got to do with struts framework though.
Thanks for correcting.
Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
Hi Mr Ankit Garg,
I'd like to do something like that on my cart page:



I've tried using this



but it doesn't seem to work, it gives an error message (resource not found) . I always end up getting the medicine ID and the quantity.
Anyway my school project suggests this:

Information used by several HTML/JSP pages (i.e. the shopping cart) should be stored in the session object. It should not be stored in hidden fields and posted from page to page.

What is the thing that I need to do before using this:



Thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Jia I think you should modify your design. There must be no scripting in JSPs. You must create a servlet to handle all the business logic. The JSPs must call the servlets to handle the request processing. The servlet would then handle the business logic. I am not talking about the type of controller servlet that you are using. I am talking about a new servlet to handle business logic. Just google with the tern shopping cart java and you'll get a ton of helpful tutorial on the internet with the right way of creating a shopping cart...
Jia Bin Tang
Greenhorn

Joined: Mar 06, 2008
Posts: 16
Ankit Garg wrote: There must be no scripting in JSPs.


Hi, sorry for reviving old thread again, but I don't quite understand this part.
I saw some books that even writes the business logics on the JSP page itself (though I don't prefer to do that).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

Jia Bin Tang wrote:
Ankit Garg wrote: There must be no scripting in JSPs.


Hi, sorry for reviving old thread again, but I don't quite understand this part.

Modern best practices dictate that JSPs should only be used as units of view. Any logic should be sequestered Java classes delegated by controllers. If you have not read this article yet, it may be helpful.

I saw some books that even writes the business logics on the JSP page itself (though I don't prefer to do that).

These books should be burned!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shopping cart help
 
Similar Threads
submit button is not working
creating a log file for my servlet
Using datasource for login bean & login page?
unable to forward using jsp:forward
Help with this code!