File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes help for shopping cart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help for shopping cart" Watch "help for shopping cart" New topic

help for shopping cart

bibin sk

Joined: Mar 04, 2001
Posts: 6
dear ones
i am doing project in online project contains store for books,cars,watches problem is how to develop a shopping cart which shows deatils about items selected when user clicks addto bag
here is my sample html page which is a bookstore
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#FFFFCC" text="#000000">
<font size="+1">1. Java Servlets</font>
<pre align="left"><font size="+1"> <b>Author :Karl Moss
Price :275.00
Publisher :
ID :12457 <font size="-1"> <a href="/servlet/showbag?bookid=12457&bname=java servlets&price=275.00" target="_blank" >Add to Bag</a></font></b></font></pre>
<p align="left"><font size="+1">2.Java Server Pages</font></p>
<div align="left">
<pre><font size="+1"><b>Author :
Price :
Publisher :
ID : 12359 <a href="/servlet/showbag?bookid=12359&bname=Java Server Pages&price=456.00" target="_blank"><font size="-1">Add to Bag</font></a></b></font></pre>
<h3><font size="+1">3.Programming in C</font></h3>
<pre><font size="+1"><b>Author :Balaguruswamy
P</b></font><b><font size="+1">rice :150.00
Publisher :</font>
<font size="+1">ID :12358 <font size="-1"> <a href="/servlet/bk1?bookid=12358" target="_blank">Add to Bag</a></font></font></b></pre>
<p><b><font size="+1">4.Visual C++</font></b></p>
<pre><b><font size="+1">Author :
</font></b><b><font size="+1">Price :
</font></b><b><font size="+1">Publisher :</font></b>
<b><font size="+1">ID :12369 <a href="/servlet/bk1?bookid=12369" target="_blank"><font size="-1">Add to bag</font></a></font></b></pre>
<h2>5.Computer Networks</h2>
<pre><font size="3"><b><font size="+1">Author :Tannenbaum
Price :200
ID : 12457 </font><font size="3"><b><font size="+1"> <font size="-1"><a href="/servlet/bk1?bookid=12459" target="_blank">Add to bag</a></font></font></b></font></b></font></pre>
<pre><font size="3"><b><font size="+1"> </font><font size="3"><b><font size="3"><b><font size="+1"><a href="page3.htm" target="_blank"><img src="home.jpg" width="160" height="34" border="0"></a></font></b></font></b></font></b></font></pre>
ihave written one servlet for shopping cart but shows error500
the servler is
package onlineshop;
// import online.BooksDetails;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowCartServlet extends HttpServlet
public BooksDetails cart;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException

PrintWriter out = response.getWriter();
HttpSession session=request.getSession(true);

if (cart == null)
cart = new BooksDetails();
session.putValue(session.getId(), cart);

private void addCart(HttpServletRequest request)
int units=1;
String bookid=request.getParameter("bookid");
double price=Double.valueOf(request.getParameter("price")).doubleValue();
String bname =request.getParameter("bname");
private void showCart(PrintWriter out)
//out.println("<center><br><p><font color=red>Your user ID: "+cart.cid+"<br><br>");
// show empty info if there's nothing in shopping cart
if (cart.totalItems==0){
out.println("<h3><font color=yellow>Sorry, there's nothing in your shopping cart yet!</font></h3>");}
else {
out.println("<center><table border=4 background=\"/bg.gif\" width = 90% >");
out.println("<td><b><font color=red><center>Bookname</center></font></b></td>");
out.println("<td><b><font color=red><center>Bookid</center></font></b></td>");
out.println("<td><b><font color=red><center>Price</center></font></b></td>");
out.println("<td><b><font color=red><center>Quantity</center></font></b></td>");
out.println("<td><b><font color=red><center>SubAmount</center></font></b></td>");
for (int i = 0; i < cart.totalItems; i++){
out.println("<td><b><font color=blue><center>   "+cart.bname[i]+"   </center></font></b></td>");
out.println("<td><b><font color=blue><center> "+cart.bookid[i]+" </center></font></b></td>");
out.println("<td><b><font color=blue><center> "+cart.unitprice[i]+" </center></font></b></td>");
out.println("<td><b><font color=blue><center> $"+cart.units[i]+" </center></font></b></td>");
out.println("<td><b><font color=blue><center> $"+cart.stotal[i]+" </center></font></b></td>");

// package online;
// import*;
// import java.util.*;
class BooksDetails
public String[] bname;
public String[] author;
public String[] bookid;
public int[] units;
public double[] unitprice;
public double[] stotal;
public double totalPrice =0;
public int cid=0;
public String password="";
public int maxItems = 100;
public int totalItems =0;

public void BooksDetails()
bname=new String[maxItems];
author=new String[maxItems];
units=new int[maxItems];
bookid=new String[maxItems];
stotal=new double[maxItems];
public BooksDetails additem(String bname,String bookid,int units,double unitprice)
return this;
public boolean found(String bookid)
for(int i=0;i<totalItems;i++)
if (this.bookid[i].equals(bookid))
return true;
return false;
public String[] showCart() {
String[] s = new String[totalItems];
for (int i = 0; i < totalItems; i++)
s[i] = "<td><center>   "+bname[i]+"   </center></td>";
s[i] += "<td><center> "+bookid[i]+" </center></td>";
s[i] += "<td><center> $"+unitprice[i]+" </center></td>";
s[i] += "<td><center><input type=\"text\" size=\"5\" name=\"items\"> "+units[i]+" </center></td>";
s[i] += "<td><center> $;"+stotal[i]+" </center></td>";
return s;
public double getPrice() {
double p = 0.0;
for (int i = 0; i < totalItems; i++){
p += units[i]*unitprice[i];
return p;
please help m with correction if possible or a new source code for the shopping cart
I agree. Here's the link:
subject: help for shopping cart
It's not a secret anymore!