Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store multiple items into a shopping cart?

 
kripa ganesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have been trying to add items to the cart using servlets. Only the recent item added is displayed. How is it possible to add multiple products to the cart and once the user clicks the add to cart, the linked servlet should display the items (if already added ) alongwith the recent item added..
Any one can you pls help me with a sample code...
Thanx in advance,
kripa
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will assume you are using some kind of session tracking and a collection to hold the items in your cart. can you post some code and lets see where you are going wrong?
 
kripa ganesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Pls find below the code i used for the cart items.
The qty is entered in a previous servlet and passed to this servlet. Here if the qty is 0 then no item should be displayed. If there are more than one items then it should list all of them.
Pls Help!! AM Stuck with this code. IS there any other alternative way to do it..Pls help..
package imshoppe;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Hashtable;
import java.text.NumberFormat;
import java.lang.*;
public class ShoppingCart extends HttpServlet {
private Connection dbc;
public void init(ServletConfig config) throws ServletException {
super.init(config); dbc = null;
// connecting to database
Statement stmt = null;
ResultSet rs = null;
String host = "localhost";
String user="root";
String pass="";
String db="shop";
String con;
try {
Class.forName("org.gjt.mm.mysql.Driver");
con="jdbc:mysql://" + host + "/" + db +"?user=" + user +"&password="+pass;
Connection Conn = DriverManager.getConnection(con);
stmt = Conn.createStatement();

} catch (Exception e) {
e.printStackTrace();
}
}
public void destroy()
{
try
{ dbc.close();
}
catch (SQLException e)
{ e.printStackTrace(); }
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Hashtable cart;
PrintWriter out;
out = response.getWriter();
response.setContentType("text/html");
HttpSession session = request.getSession(true);
String id = request.getParameter("prodid");
String command = request.getParameter("command");
String sessionId = request.getParameter("sessionId");
String uqty = request.getParameter("uqty");
String requestURI = request.getRequestURI();
cart = (Hashtable) session.getValue("cart");
if (cart == null)
{
cart = new Hashtable();
}
out.println("<h1>Shopping Cart</h1>");
out.print("Request URI: "+request.getRequestURI());
out.print("<br>");
out.print("The Product ID retrieved from previous servlet:" + id);
out.print("<br>");
out.print("The Session ID retrieved: " + sessionId);
out.print("<br>");
out.print("The Command retrieved: " + command);
out.print("<br>");
out.print("The qty ordered : " + uqty);
out.print("<br>");
if
(command.equals("add"))
{
Integer num = (Integer) cart.get(id);
if (num == null) {
num = new Integer(0);
}
session.setAttribute(id, new Integer(num.intValue()+1) );
//printCatalog(out, requestURI);
out.println("Item Added to Cart");
}
else if
(command.equals("viewcart"))
{
printCart(cart, out, requestURI);
}
else
{
out.println("Back to Shopping");
//printCatalog(out, requestURI); }
session.setAttribute("cart", cart); }
}
private void printCart(Hashtable cart, PrintWriter out, String requestURI) throws
ServletException, IOException
{
Statement stmt;
ResultSet rs=null;
double total = 0.0;
//Integer prod = rs.getInt("prodid");
out.println("<td><a href=\"" + requestURI + "?command=viewcart"+ "\">View Cart</a></td>");
Enumeration keys = cart.keys();
NumberFormat nf = NumberFormat.getCurrencyInstance();
out.println("Your cart contains:");
out.println("");
out.println("Quantity Item Price Subtotal ");
while (keys.hasMoreElements())
{
String id = (String) keys.nextElement();
Integer qty = (Integer) cart.get(id);
try {
stmt = dbc.createStatement();
stmt.execute("select * from prods where pid = " + id);
rs = stmt.getResultSet();
double subtotal = rs.getDouble("rat") * qty.intValue();
out.println("");
out.println("" + qty.toString() + "");
out.println("" + rs.getString("pnam") + "");
out.println("" + nf.format(rs.getDouble("rate")) + "");
out.println("" + nf.format(subtotal ) + "");
out.println(" ");
total += subtotal;
rs.close();
stmt.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
out.println("TOTAL:" + nf.format(total) + "");
out.println("");
out.println("Back to Shopping");
}
}
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't run the program, no server in my pc now
but here are the things I noted..
1. use session.getAttribute()
2. Where are you adding the element to cart ? I dont think you are adding to hashtable..
3. Why have you put the database query within the while loop for hashtable. Its making query to database everytime. isn't it??
More and exact things if i get my server.
Cheers
 
kripa ganesh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx
I tried out what u said, but am sorry it didnt work. can someone help me with a sample code using hash table, so that i can follow the same and correct mine.
 
Hans Soderstrom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as Lakshmeenarayana Goundalkar is trying to say
session.getValue is depricated (but it is working for now).
What you need to do in the code is to change this
-----------------------------
if
(command.equals("add"))
{
Integer num = (Integer) cart.get(id);
if (num == null) {
num = new Integer(0);
}
cart.put(id,new Integer(num.intValue()+1));
session.setAttribute("cart",cart);
//printCatalog(out, requestURI);
out.println("Item Added to Cart");
}
------------------------------------
this should add the cart with the new item in the session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic