• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Shopping cart help

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Jia Bin Tang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic