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");
}
}