What is wrong with my servlet?

What is wrong with my servlet?

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");
View Answers









Related Tutorials/Questions & Answers:
What is wrong with my servlet?
what is wrong with my JSP codes for updating a form?
Advertisements
What's wrong with my form? - Java Beginners
What's wrong with my pagination code in JSP?
servlet7
servlet3
servlet4
servlet5
servlet2
servlet6
servlet6
What is wrong with this line
what is wrong with this program
what is wrong in this program
what is wrong in this program......
servlet1
servlet1
servlet1
servlet1
servlet1
servlet1
servlet1
servlet1
servlet1
servlet1
Not sure whats wrong with my code HELP PLEASE?!?!
please check my code is wrong or ok.it was not work .this is form validation in javascript using jsf page
what is wrong in this program?I have netbeans 7.0 & jdk 7. please do corrections.
servlets
servlets
What is the best way to learn Java on my own?
servlets
servlets
What is Java Servlets?
servlets
servlets
ModuleNotFoundError: No module named 'wrong'
servlets
wrong year
how do i provide down a pdf document fecility on my web page using jsp and servlets?
servlets
Servlets
servlets
servlets
the servlets
servlets
Servlets
Servlets
servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets

Ads