Hi, I'd like to construct a on-line store program by using Java servlet/JSP. Can anyone give me some good website on how to construct it step by step? Or is there any good sample code on-line? Thanks a lot!
I would first suggest that you go to one of the online stores and take a look at the behaviour of their shopping carts. I tried www.amazon.com and www.buycds.com and I liked what I saw.
Here is what i believe you need:
1. A JavaBean which will act as your ShoppingCart Object 2. A database to hold your products 3. A Servlet to act as a controller (e.g. a view could call the servlet when adding/removing items from your cart 4. A JSP which can act as a 'View'. This jsp will allow you see the current state of the cart. 5. 1 or more jsps which will allow you to interact with the servlet (see point 3).
Here is how it all works:
This is called MVC.
You have a jsp that gets the items from your db and displays them on a table. You provide a link on this table which also embeds information such as productId etc so that you can forward this information to other pages. Call this jsp something like 'AddToShoppingCart.jsp'
When you click the 'Add' button on the 'AddToShoppingCart.jsp' it will then forward all the data to the 'ShoppingCartControllerServlet.java' file
The 'ShoppingCartControllerServlet.java' file merely takes the commands like 'add, remove or updateQuantity' from any of the jsps and processes them by calling the ShoppingCart class/Object. After it has done it's job it should automatically forward you back to the view (viewShoppingCart.jsp) so you can see the state of your cart.
The viewShoppingCart.jsp could use a nicely formatted table to display your cart and have buttons such as 'remove' or updateQuantity.
Well, that was just a brief explanation to give you an idea. There are a lot more issues involved here like making sure that you keep a ShoppingCart object in a session while the user is still shopping so that things don't get mixed up. You then have to allow the user to check-out like in a normal shop and not to mention the credit card handling mechanism.
I have done this for myself just out of curiosity and i would be happy to show you some of my code.
Good luck!! [This message has been edited by ernest fakudze (edited October 15, 2001).]
In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
You can use Struts as the application framework to make things a little easier for you if you want http://jakarta.apache.org/struts/ Keep your eye on MyCart http://mycart.cjb.net/ because they might actually get it done one day. Or check out this article: http://www.java-pro.com/upload/free/features/javapro/2000/06jun00/gb0006/gb0006.asp Plus you can have a look at the Java Pet Store from Sun (look up the URL your self!) which illustrates what they think are "best practices" for J2EE applications. However, this might be overkill for what you want since it uses EJB's which aren't really neccessary for small scale developments amongst other things. And of course there is google.com which will find you about a million other examples if you ask it nicely. Andre.