Hey everybody,
I hope somebody can help me.I am trying to create an online shopping cart that uses sessions. So i have a
servlet side and a class Product and the client side 'ShopCart'. I have 2 errors..one is in the ShopCart.jsp file where it cannot resolve symbol 'class Product' and the other error is ShopCart.jsp uses or overrides deprecated API.
Here is ShopCart which is -->
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>
<%@page import = "java.util.*"%>
<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
if (buylist != null && (buylist.size() > 0)) {
%>
<center>
<table border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>
<td><b>ProductId</b></td>
<td><b>Product Name</b></td>
<td><b>Price</b></td>
<td><b>Quantity</b></td>
<td></td>
</tr>
<%
for (int index=0; index < buylist.size();index++) {
Product anOrder = (Product) buylist.elementAt(index);
%>
<tr>
<td><b><%= anOrder.getProductId() %></b></td>
<td><b><%= anOrder.getPName() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
<td>
<form name="deleteForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="submit" value="Delete">
<input type="hidden" name= "delindex" value='<%= index %>'>
<input type="hidden" name="action" value="DELETE">
</form>
</td>
</tr>
<% } %>
</table>
<p>
<form name="checkoutForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="hidden" name="action" value="CHECKOUT">
<input type="submit" name="Checkout" value="Checkout">
</form>
</center>
<% } %>
Here is my class Product -->
package shopping.Product;
public class Product {
String productId;
String PName;
float price;
int quantity;
/** Creates a new instance of Product */
public Product() {
productId="";
PName="";
price=0;
quantity=0;
}
public void setProductId(String title){
productId=title;
}
public String getProductId(){
return ProductId;
}
public String getPName(){
return PName;
}
public void setPrice(float p){
price=p;
}
public void setQuantity(){
return quantity;
}
}
And Finally Here is my Servlet -->
package shopping.Product;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author HarringtonD
* @version
*/
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
res.sendRedirect("http://localhost:8084/sampledir/errorCart.html");
}
Vector buylist=
(Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
Product aProduct = getProduct(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector(); //first order
buylist.addElement(aProduct);
} else { // not first buy
for (int i=0; i< buylist.size(); i++) {
Product prod = (Product) buylist.elementAt(i);
if (prod.getProductId().equals(aProduct.getProductId())) {
prod.setQuantity(prod.getQuantity()+aProduct.getQuantity());
buylist.setElementAt(prod,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(aProduct);
}
}
session.putValue("shopping.shoppingcart", buylist);
String url="http://localhost:8084/sampledir/ShopCart.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
float total =0;
for (int i=0; i< buylist.size();i++) {
Product anOrder = (Product) buylist.elementAt(i);
float price= anOrder.getPrice();
int qty = anOrder.getQuantity();
total += (price * qty);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf('.');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="http://localhost:8084/sampledir/checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private Product getProduct(HttpServletRequest req) {
String myProduct = req.getParameter("Product");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myProduct,"|");
String productId= t.nextToken();
String PName = t.nextToken();
String price = t.nextToken();
price = price.replace('$',' ').trim();
Product prod = new Product();
prod.setProductId(productId);
prod.setPName(PName);
prod.setPrice((new Float(price)).floatValue());
prod.setQuantity((new Integer(qty)).intValue());
return prod;
}
}
I hope somebody can help me., I cant see these erros..sorry about all the code. thanx.