Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

exercise that uses hidden forms for session tracking

Thread view: 
ros - 11 Apr 2007 21:58 GMT
Hello all,

I am working on an exercise ( yes, it is an exercise) that has the
following requirements:

-Create a new servlet called RemoveItemsFromCart.java.

-Get it working with ReviewShoppingCart.java and
AddToShoppingCart.java.

-You will need to edit ReviewShoppingCart.java to add another button
that calls RemoveItemsFromCart.java

I have created the new servlet and the three are pasted below.

But my problem is that when I click the Remove button nothing happens.
I uncheck the checkboxes but when I hit Remove, they come back
selected.

I would be really thankful if someone guides me on this exercise.
Causing me a lot of headache. Please advise if I am following an
incorrect approach and if so what I should do.

Thanks in advance.
ros

==================================================================================

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

       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>" );
   }

}

==================================================================================

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

       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>" );
   }
}
==================================================================================
Tim B - 12 Apr 2007 03:13 GMT
> But my problem is that when I click the Remove button nothing happens.
> I uncheck the checkboxes but when I hit Remove, they come back
> selected.

I have admittedly not looked at your code, but your problem is probably
caused by the fact that no request parameters are sent by the browser for
checkboxes that are not checked. You can get around this by using hidden
fields with values set by javascript, or if you know all the names of the
checkboxes, you can determine which parameters are missing from the request
and deal with them accordingly.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.