I'm trying to implement like a small shopping cart system where there is a variety of items to choose from. There are 3 JSP pages, shop1.jsp , shop2.jsp and shop3.jsp where each of these pages have different items to choose from. And there is another page final.jsp which displays all of the items selected. The selection of items is made using checkboxes.
The flow of pages goes like this : shop1.jsp -> shop2.jsp -> shop3.jsp -> final.jsp and there is also a hyperlink on each page (excluding shop1.jsp) which allows user to go back to the previous page as well.
If an item was previously selected by the client, the suitable check box should be checked and vice versa. I need to make use of a stateful session bean to store the items selected by the client.
NOTE:
I remove all possible items that is supposed to be on each page(shop1, shop2 and shop3) if the arraylist contains them of course before making a fresh new add, since adding is appending to the arraylist, it will contain other previous items added if they are not removed.
I submit the page on itself which would cause a redirect afterward using the response.sendRedirect() method.
Here are the codes(the jsp codes are too long, so I uploaded them in my dropbox, I provided the links below) and after that, the issue I'm having:
Stateful session bean CartBean
package shop; import javax.ejb.LocalBean; import javax.ejb.Stateful; import java.util.ArrayList; import java.util.Collection; @Stateful @LocalBean public class CartBean { private ArrayList <String> cart = new ArrayList<String>(); public CartBean() {} public void addItem(String item){ cart.add(item); } public void removeItem(String item) { cart.remove(item); } public Collection <String> getItems() { return cart; } }
shop1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.util.ArrayList,java.util.Arrays" %> <jsp:useBean id="cart" class="shop.CartBean" scope="session" /> <%! Boolean isSubmitted1 = false; String[] ItemNames1; String[] allItems = {"Item 1" , "Item 2" , "Item 3" , "Item 4" , "Item 5"}; String checkBox1 = ""; String checkBox2 = ""; String checkBox3 = ""; String checkBox4 = ""; String checkBox5 = ""; %> <% isSubmitted1 = Boolean.parseBoolean(request.getParameter("Submitted1")); if (isSubmitted1) { for (int i=0 ; i<allItems.length ; i++) { if (cart.getItems().contains(allItems[i])) { cart.removeItem(allItems[i]); } } if (request.getParameterValues("checkBoxes1") != null) { ItemNames1 = (String[]) request.getParameterValues("checkBoxes1"); if (ItemNames1.length > 0) { for (int i=0 ; i<ItemNames1.length ; i++) { String currentItem = ItemNames1[i]; cart.addItem(currentItem); } } } response.sendRedirect("shop2.jsp"); } else { if (!cart.getItems().isEmpty()) { if (cart.getItems().contains("ITEM 1")) { checkBox1 = "CHECKED"; } if (cart.getItems().contains("ITEM 2")) { checkBox2 = "CHECKED"; } if (cart.getItems().contains("ITEM 3")) { checkBox3 = "CHECKED"; } if (cart.getItems().contains("ITEM 4")) { checkBox4 = "CHECKED"; } if (cart.getItems().contains("ITEM 5")) { checkBox5 = "CHECKED"; } } // end if (!cart.getItems().isEmpty()) } // end else %> <html> <body> <form action="shop1.jsp" method="post"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><font size="16">Select Items: </font></td> </tr> <tr> <td align="center"> <input type="checkbox" name="checkBoxes1" value="ITEM 1" <%= checkBox1 %> /> ITEM 1.<br> <input type="checkbox" name="checkBoxes1" value="ITEM 2" <%= checkBox2 %> /> ITEM 2.<br> <input type="checkbox" name="checkBoxes1" value="ITEM 3" <%= checkBox3 %> /> ITEM 3.<br> <input type="checkbox" name="checkBoxes1" value="ITEM 4" <%= checkBox4 %> /> ITEM 4.<br> <input type="checkbox" name="checkBoxes1" value="ITEM 5" <%= checkBox5 %> /> ITEM 5.<br><br> </td> </tr> <tr> <td><input type="submit" value="Next Page" /></td> </tr> <tr> <td><input type="reset" value="Reset all" /></td> </tr> </table> <input type="hidden" name="Submitted1" value="true" /> </form> </body> </html>
shop2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.util.ArrayList,java.util.Arrays" %> <jsp:useBean id="cart" class="shop.CartBean" scope="session" /> <%! Boolean isSubmitted2 = false; String[] ItemNames2; String[] allItems = {"Item 6" , "Item 7" , "Item 8" , "Item 9" , "Item 10"}; String checkBox6 = ""; String checkBox7 = ""; String checkBox8 = ""; String checkBox9 = ""; String checkBox10 = ""; %> <% isSubmitted2 = Boolean.parseBoolean(request.getParameter("Submitted2")); if (isSubmitted2) { for (int i=0 ; i<allItems.length ; i++) { if (cart.getItems().contains(allItems[i])) { cart.removeItem(allItems[i]); } } if (request.getParameterValues("checkBoxes2") != null) { ItemNames2 = (String[]) request.getParameterValues("checkBoxes2"); if (ItemNames2.length > 0) { for (int i=0 ; i<ItemNames2.length ; i++) { String currentItem = ItemNames2[i]; cart.addItem(currentItem); } } } response.sendRedirect("shop3.jsp"); } else { if (!cart.getItems().isEmpty()) { if (cart.getItems().contains("ITEM 6")) { checkBox6 = "CHECKED"; } if (cart.getItems().contains("ITEM 7")) { checkBox7 = "CHECKED"; } if (cart.getItems().contains("ITEM 8")) { checkBox8 = "CHECKED"; } if (cart.getItems().contains("ITEM 9")) { checkBox9 = "CHECKED"; } if (cart.getItems().contains("ITEM 10")) { checkBox10 = "CHECKED"; } } // end if (!cart.getItems().isEmpty()) } // end else %> <html> <body> <form action="shop2.jsp" method="post"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><font size="16">Select Items: </font></td> </tr> <tr> <td align="center"> <input type="checkbox" name="checkBoxes2" value="ITEM 6" <%= checkBox6 %> /> ITEM 6.<br> <input type="checkbox" name="checkBoxes2" value="ITEM 7" <%= checkBox7 %> /> ITEM 7.<br> <input type="checkbox" name="checkBoxes2" value="ITEM 8" <%= checkBox8 %> /> ITEM 8.<br> <input type="checkbox" name="checkBoxes2" value="ITEM 9" <%= checkBox9 %> /> ITEM 9.<br> <input type="checkbox" name="checkBoxes2" value="ITEM 10" <%= checkBox10 %> /> ITEM 10.<br><br> </td> </tr> <tr> <td><input type="submit" value="Next Page" /></td> </tr> <tr> <td><input type="reset" value="Reset all" /></td> </tr> </table> <input type="hidden" name="Submitted2" value="true" /> </form> </body> </html>
shop3.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.util.ArrayList,java.util.Arrays" %> <jsp:useBean id="cart" class="shop.CartBean" scope="session" /> <%! Boolean isSubmitted3 = false; String[] ItemNames3; String[] allItems = {"Item 11" , "Item 12" , "Item 13" , "Item 14" , "Item 15"}; String checkBox11 = ""; String checkBox12 = ""; String checkBox13 = ""; String checkBox14 = ""; String checkBox15 = ""; %> <% isSubmitted3 = Boolean.parseBoolean(request.getParameter("Submitted3")); if (isSubmitted3) { for (int i=0 ; i<allItems.length ; i++) { if (cart.getItems().contains(allItems[i])) { cart.removeItem(allItems[i]); } } if (request.getParameterValues("checkBoxes3") != null) { ItemNames3 = (String[]) request.getParameterValues("checkBoxes3"); if (ItemNames3.length > 0) { for (int i=0 ; i<ItemNames3.length ; i++) { String currentItem = ItemNames3[i]; cart.addItem(currentItem); } } } response.sendRedirect("final.jsp"); } else { if (!cart.getItems().isEmpty()) { if (cart.getItems().contains("ITEM 11")) { checkBox11 = "CHECKED"; } if (cart.getItems().contains("ITEM 12")) { checkBox12 = "CHECKED"; } if (cart.getItems().contains("ITEM 13")) { checkBox13 = "CHECKED"; } if (cart.getItems().contains("ITEM 14")) { checkBox14 = "CHECKED"; } if (cart.getItems().contains("ITEM 15")) { checkBox15 = "CHECKED"; } } // end if (!cart.getItems().isEmpty()) } // end else %> <html> <body> <form action="shop3.jsp" method="post"> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td><font size="16">Select Items: </font></td> </tr> <tr> <td align="center"> <input type="checkbox" name="checkBoxes3" value="ITEM 11" <%= checkBox11 %> /> ITEM 11.<br> <input type="checkbox" name="checkBoxes3" value="ITEM 12" <%= checkBox12 %> /> ITEM 12.<br> <input type="checkbox" name="checkBoxes3" value="ITEM 13" <%= checkBox13 %> /> ITEM 13.<br> <input type="checkbox" name="checkBoxes3" value="ITEM 14" <%= checkBox14 %> /> ITEM 14.<br> <input type="checkbox" name="checkBoxes3" value="ITEM 14" <%= checkBox15 %> /> ITEM 15.<br><br> </td> </tr> <tr> <td><input type="submit" value="Next Page" /></td> </tr> <tr> <td><input type="reset" value="Reset all" /></td> </tr> </table> <input type="hidden" name="Submitted3" value="true" /> </form> </body> </html>
final.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.util.ArrayList" %> <jsp:useBean id="cart" class="shop.CartBean" scope="session" /> <% ArrayList <String> items = (ArrayList <String>) cart.getItems(); %> <html> <body> <table align="left" border="1" cellpadding="0" cellspacing="0"> <tr> <td> <font size="16"> <% if (items.size()==0) { %> You have not selected any items: <% } else { %> You have selected the following items: <% } %> </font> </td> </tr> <% for (int i=0 ; i<items.size() ; i++) { %> <tr> <td><%= items.get(i) %></td> </tr> <% } %> <tr> <td><a href="shop3.jsp">Back to shop3</a></td> </tr> </table> </body> </html>
Here an example of the issue I'm having since I find it hard to explain in general for all:
Lets say I am on shop1.jsp , I select "ITEM 3" only and submit.
Now I am on shop2.jsp, I click on the hyperlink to go "back to shop 1".
I find the checkbox "ITEM 3" checked, this is alrite since i previously selected it and the item value is stored in the Stateful bean arraylist.
Now on shop1.jsp, I uncheck that checkbox "ITEM 3" and submit.
I'm now on shop2.jsp and I go back to shop1.jsp again using the hyperlink.
I find the checkbox "ITEM 3" still checked while it should not have been.
Can anyone help find what is wrong?
Ads