Share on Google+Share on Google+

What is wrong with my servlet?
0 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners

I'd like to know which part of the code is wrong. My code is:

print("code sample");
import java.util.*;
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 {
      public void init(ServletConfig conf) throws ServletException {
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        if(session == null){
        Vector buylist = (Vector)session.getValue("shopping.shoppingcart");
        String action = request.getParameter("action");
                String del = request.getParameter("delindex");
                int d = (new Integer(del)).intValue();
            } 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

 }else { // not first buy
                for(int i =0; i < buylist.size(); i++){
                    CD cd = (CD)buylist.elementAt(i);
                        cd.setQuantity(cd.getQuantity() + aCD.getQuantity());
                        buylist.setElementAt(cd, i);
                        match = true;
                    }// end of if name matches
                }// end of for
                if (!match)
        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.setPrice((new Float(price)).floatValue());
        cd.setQuantity((new Integer(qty)).intValue());
        return cd;

print("code sample");


View Answers

Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized


Advertisement null