I'd like to know which part of the code is wrong. My code is:
print("code sample");
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import shopping.CD;
public class ShoppingServlet extends HttpServlet {
@Override
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if(session == null){
respond.sendRedirect("error.html");
}
Vector buylist = (Vector)session.getValue("shopping.shoppingcart");
String action = request.getParameter("action");
if(!action.equals("CHECKOUT")){
if(action.equals("DELETE")){
String del = request.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;
CD aCD = getCD(request);
if (buylist == null) {
// add first cd to the cart
buylist = new Vector(); // first order
buylist.addElement(aCD);
}else { // not first buy
for(int i =0; i < buylist.size(); i++){
CD cd = (CD)buylist.elementAt(i);
if(cd.getAlbum().equals(aCD.getAlbum())){
cd.setQuantity(cd.getQuantity() + aCD.getQuantity());
buylist.setElementAt(cd, i);
match = true;
}// end of if name matches
}// end of for
if (!match)
buylist.addElement(aCD);
}}
session.putValue("shopping.shoppingcart", buylist);
String url="/Eshop.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
} else if(action.equals("CHECKOUT")){
float total=0;
for(int i=0; i < buylist.size(); i++){
CD anOrder = (CD)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);
request.setAttribute("amount", amount);
String url="Checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
}
}
private CD getCD(HttpServletRequest req){
String myCd = req.getParameter("CD");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myCd, "|");
String album = t.nextToken();
String artist = t.nextToken();
String country = t.nextToken();
String price = t.nextToken();
price = price.replace('$', ' ').trim();
CD cd = new CD();
cd.setAlbum(album);
cd.setArtist(artist);
cd.setCountry(country);
cd.setPrice((new Float(price)).floatValue());
cd.setQuantity((new Integer(qty)).intValue());
return cd;
}
}
print("code sample");