|
 |
pro_jsp thread: Couldn't increase the counter ??
Message #1 by "Low Kok Leong" <lousai@t...> on Mon, 4 Dec 2000 10:30:51 -0000
|
|
Hi , L'm trying to modify the Sun microsystems example. But I couldn't
increase the counter. First I display the item thru catalog.jsp there user
can look the book details or click add to cart.
which lead to a servlet called ShoppingCart.java and
ShoppingCartItem.java.
below I attach the sample code .Please help me up. T.Q
----------------code--------------------------------------
1.catalog.jsp
<HTML>
<HEAD>
<TITLE>Lousai</TITLE>
</HEAD>
<BODY>
<%@ page language="java" import="java.sql.*" %>
<H3>Pasar Malam Product Catalog</H3>
<b>Category: Book->Health <b><p>
<jsp:useBean id='FAQBean' scope='page' class='CustomerService.FAQBean' type="CustomerService.FAQBean" />
<jsp:useBean id='SCART' scope='page' class='scart.ShoppingCart' type="scart.ShoppingCart" />
<P><EM>
<%
String [] values = request.getParameterValues("Buy");
if(values != null) {
String value = values[0];
try {
out.println(value);
out.println(FAQBean.DBConnect());
ResultSet RS = FAQBean.executeQuery("SELECT * FROM QA WHERE
bookID ='" + value + "' ");
out.println("****ed");
SCART.add (value);
out.println("****");
out.println("<br>");
if(RS == null)
out.println("<P>There are no FAQ entries for This </P>");
else
{
int count = 1;
while (RS.next())
{
String t1 = RS.getString("description");
out.println("You have just added" );
out.println("<BR>");
out.println("<STRONG><FONT COLOR=#D9D9F3>" + t1 + "</STRONG>" );
}
}
}
catch (SQLException e) {
out.println(" e.getMessage");
}
}else {
out.println("Please select from the choice, Thank You");
}
%>
<BR>
<%
int num = SCART.getNumberOfItems( );
// out.println(num);
if ( num > 0 ) {
out.println("<BR>");
out.println("you have " + num + " in your shopping basket");
}else {
out.println("Click Add To Cart to put item in your basket");
}
%>
<%
try {
out.println(FAQBean.DBConnect());
ResultSet RS = FAQBean.executeQuery("SELECT * FROM book ");
if(RS == null)
out.println("<P>There are no FAQ entries for This </P>");
else
{ //-----------------------------------------------------------
// Print out the Questions and link them to their answers below
//-----------------------------------------------------------
int count = 1;
out.println("<TABLE BORDER=0 WIDTH=600><TR BGCOLOR=#D9D9F3><TD><B><OL>");
while (RS.next())
{
out.println();
out.println("<TR>");
String id = RS.getString("bookID");
String detailurl
response.encodeUrl("bookdetails.jsp?bookID=" + id);
out.println("<TD BGCOLOR=#D9D9F3>" + id + "</TD>");
String t = RS.getString("title");
out.println("<TD BGCOLOR=#D9D9F3><a href=" + detailurl + ">" + t + "</a></TD>");
out.println("<TD BGCOLOR=#D9D9F3><EM>" + RS.getString("price") + "</EM></TD>");
String carturl = response.encodeUrl("catalog.jsp?Buy=" + id);
String addcart = "Add To Cart";
out.println("<TD BGCOLOR=#D9D9F3><a href=" + carturl + ">" + addcart + "</a></TD>");
out.println("</TR>");
//out.println( RS.getString("Category") );
//out.println("<LI><A HREF=\"#" + count + "\">" + RS.getString("Question") + "</A>");
//out.println("<LI><A HREF=\"#" + count + "\">" + RS.getString("Answer") + "</A>");
count++;
}
out.println("</TD></TR></TABLE></OL></B>");
}
}catch ( Exception e) {
out.println(e.getMessage());
}
%>
</BODY></HTML>
-----------------------------end catalog.jsp------------------
2.ShoppingCart.java
package scart;
import java.util.*;
import java.io.*;
import java.sql.*;
public class ShoppingCart {
Hashtable items = null;
int numberOfItems = 0;
public ShoppingCart() {
items = new Hashtable( );
}
public void add(String bookID) throws SQLException {
if(items.containsKey(bookID)) {
System.out.println("Good");
ShoppingCartItem scitem = (ShoppingCartItem)
items.get(bookID);
scitem.incrementQuantity();
} else {
System.out.println("New entry");
System.out.println(bookID);
BookDetails bd = new BookDetails(bookID);
System.out.println(bd.getTitle() );
ShoppingCartItem scitem = new ShoppingCartItem(bd);
items.put(bookID, scitem);
// scitem.incrementQuantity();
//String result = (String) items.get(bookID);
System.out.println("result");
}
numberOfItems = numberOfItems + 1;
System.out.println(numberOfItems);
System.out.println("3");
}
public void remove(String bookID) {
if(items.containsKey(bookID)) {
ShoppingCartItem scitem = (ShoppingCartItem)
items.get(bookID);
scitem.decrementQuantity();
if(scitem.getQuantity() <= 0)
items.remove(bookID);
numberOfItems--;
}
}
public Enumeration getItems() {
return items.elements();
}
protected void finalize() throws Throwable {
items.clear();
}
public int getNumberOfItems() {
return numberOfItems;
}
public void clear() {
items.clear();
numberOfItems = 0;
}
}
-----------------------end ShoppingCart.java-----------------
3.ShoppingCartItem.java
package scart;
import java.io.*;
import java.util.*;
import java.util.*;
public class ShoppingCartItem {
Object item;
int quantity;
public ShoppingCartItem(Object anItem) {
item = anItem;
quantity = 1;
System.out.println("SItem");
}
public void incrementQuantity() {
quantity++;
//System.out.println(quantity);
}
public void decrementQuantity() {
quantity--;
}
public Object getItem() {
return item;
}
public int getQuantity() {
return quantity;
}
}
-------------------------end ShoppingCartItem.java--------------
As I could see, there is no error in ShoppingCartItem.java, and I tried to
print check statement in ShoppingCart.java It seem o.k but the loop keep
not increase the value NumberOfItems each time add to cart is clicked.
|
|
 |