0% found this document useful (0 votes)
33 views4 pages

Adding and Deleting Data From Shopping Cart

The document describes three servlets for managing a shopping cart: 1) AddToShoppingCart adds items to the cart, 2) ReviewShoppingCart reviews the items in the cart, and 3) RemoveItemsFromCart allows the user to delete items from the cart. Each servlet uses hidden form fields to pass the shopping cart contents between pages.

Uploaded by

Tanya Keshari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Adding and Deleting Data From Shopping Cart

The document describes three servlets for managing a shopping cart: 1) AddToShoppingCart adds items to the cart, 2) ReviewShoppingCart reviews the items in the cart, and 3) RemoveItemsFromCart allows the user to delete items from the cart. Each servlet uses hidden form fields to pass the shopping cart contents between pages.

Uploaded by

Tanya Keshari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

adding and deleting data from shopping cart

The first servlet is to add the items to a shopping cart. The second to review those items. Then there is a third
servlet where a user is able to delete the items from the cart.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
  
public class AddToShoppingCart extends HttpServlet {
     
    public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException  {
         
        response.setContentType( "text/html" );
        PrintWriter aPW = response.getWriter();
         
        aPW.println( "<HTML><HEAD><TITLE>Shopping Basket using Hidden Forms" +
"</TITLE></HEAD><BODY>" );
         
        aPW.println( "<FORM ACTION='ReviewShoppingCart' METHOD=GET>" );
         
        aPW.println( "Add to Basket:<BR><BR>" );
         
        aPW.println( "<INPUT TYPE='checkbox' NAME='items' VALUE='Socks 
$4.0'>Socks  $4<BR>" );
         
        aPW.println( "<INPUT TYPE='checkbox' NAME='items' VALUE='Shoes 
$30.0'>Shoes  $30<BR>" );
  
        String items[] = request.getParameterValues( "items" );
  
        if ( items != null ) {
             
            for ( int i=0; i < items.length; i++ ) {
                 
       aPW.println( "<INPUT TYPE=hidden NAME=items VALUE='" + items[i] + "'>" );
                 
            }
        }
         
        aPW.println( "<BR><INPUT TYPE=submit VALUE='View Basket'>" );
    }
}

-----------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
  
public class ReviewShoppingCart extends HttpServlet {
     
    public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException  {
         
        response.setContentType( "text/html" );       
        PrintWriter aPW = response.getWriter();       
   aPW.println( "<HTML><HEAD><TITLE>Shopping Basket using Hidden Forms" +
"</TITLE></HEAD><BODY>" );       
        String items[] = request.getParameterValues( "items" );
        aPW.println( "Basket's contents:<BR><BR>" );
        if ( items == null ) {           
            aPW.println( "No items in basket yet" );           
        } else {           
            float count = 0;           
            aPW.println( "<UL>" );           
            for ( int i=0; i < items.length; i++ ) {               
                int positionOfPound = items[i].indexOf( "$" ) + 1;               
        String numberStr = items[i].substring( positionOfPound );               
                System.out.println( "positionOfPound = " + positionOfPound );
                 
                System.out.println( "numberStr = " + numberStr );               
                float price = Float.parseFloat( numberStr );               
                count = count + price;               
                aPW.println( "<LI>" + items[i] + "<input type='checkbox'
checked='checked' name='items' value='" + items[i] + "'" + " />");               
            }           
            aPW.println( "<BR><BR>Total:  $" + count );           
            aPW.println( "</UL>" );
        aPW.println( "<FORM ACTION='AddToShoppingCart' METHOD=GET>" );       
       
 if ( items != null ) {           
            for ( int i=0; i < items.length; i++ ) {               
 aPW.println("<INPUT TYPE=hidden NAME=items VALUE='"+items[i]+ "'>");               
            }
        }   
   
        aPW.println( "<BR><INPUT TYPE=submit VALUE='Add more items'>" );       
        aPW.println( "</FORM></BODY></HTML>" );       
        aPW.println( "<FORM ACTION='RemoveItemsFromCart' METHOD=GET>" );       
        if ( items != null ) {           
            for ( int i=0; i < items.length; i++ ) {               
aPW.println( "<INPUT TYPE=hidden NAME=items VALUE='" + items[i] + "'>" );               
            }
        }       
        aPW.println( "<BR><INPUT TYPE=submit VALUE='Remove items'>" );       
        aPW.println( "</FORM></BODY></HTML>" );
    }
}
--------------------------------------------

  import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
  
public class RemoveItemsFromCart extends HttpServlet {
     
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException  {   
         
        response.setContentType( "text/html" );
         
        PrintWriter aPW = response.getWriter();
         
        aPW.println( "<HTML><HEAD><TITLE>Shopping Basket using Hidden Forms" +
"</TITLE></HEAD><BODY>" );
         
        String newItems[] = request.getParameterValues( "items" );
  
        aPW.println( "Basket's contents:<BR><BR>" );
  
        if ( newItems != null ) {
             
            float count = 0;
             
            aPW.println( "<UL>" );
             
            for ( int i=0; i < newItems.length; i++ ) {
                 
                int positionOfPound = newItems[i].indexOf( "$" ) + 1;
                 
                String numberStr = newItems[i].substring( positionOfPound );
                 
                System.out.println( "positionOfPound = " + positionOfPound );
                 
                System.out.println( "numberStr = " + numberStr );
                 
                float price = Float.parseFloat( numberStr );
                 
                count = count + price;
                 
                aPW.println( "<LI>" + newItems[i] + "<input type='checkbox'
checked='checked' name='items' value='" + newItems[i] + "'" + " />");   
                 
            }
             
            aPW.println( "<BR><BR>Total:  $" + count );
             
            aPW.println( "</UL>" );
        }
  
        aPW.println( "<FORM ACTION='AddToShoppingCart' METHOD=GET>" );
         
        if ( newItems != null ) {
             
            for ( int i=0; i < newItems.length; i++ ) {
                 
                aPW.println( "<INPUT TYPE=hidden NAME=items VALUE='" + newItems[i] +
"'>" );
                 
            }
        }
         
        aPW.println( "<BR><INPUT TYPE=submit VALUE='Add more items'>" );
         
        aPW.println( "</FORM></BODY></HTML>" );
         
        aPW.println( "<FORM ACTION='ReviewShoppingCart' METHOD=GET>" );
         
        if ( newItems != null ) {
             
            for ( int i=0; i < newItems.length; i++ ) {
                 
      aPW.println( "<INPUT TYPE=hidden NAME=items VALUE='" + newItems[i] + "'>" );
             }
        }
         
        aPW.println( "<BR><INPUT TYPE=submit VALUE='Remove items'>" );
         
        aPW.println( "</FORM></BODY></HTML>" );
    }
     
}

You might also like