JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean

JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean

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?

View Answers

Ads









Related Tutorials/Questions & Answers:
Stateful and Stateless Session Bean Life Cycle
Understanding Stateful and Stateless Session Bean Life Cycle... Stateful Session Bean Life cycle There are there stages in the life cycle of Stateful Session bean Life cycle. These are: a) Does Not Exist This is the Does
can pass list of n values in session and get in jsp
can pass list of n values in session and get in jsp  In dao: am... In servlet: list=userBean.selectUserBo(); HttpSession session = request.getSession(true); session.setAttribute("currentUser",list); In jsp: <
Advertisements
can pass list of n values in session and get in jsp
can pass list of n values in session and get in jsp  In dao: am... In servlet: list=userBean.selectUserBo(); HttpSession session = request.getSession(true); session.setAttribute("currentUser",list); In jsp: <
Retain jsp values
Retain jsp values  how to retain a jsp values without using session...() and request.setAttribute() methods to get the jsp fields values. For more information..., See Session In JSP tutorial page. Thanks
Stateful Session Bean Example
Stateful Session Bean Example   ... using stateful session bean. The purpose of account is to performs two... in the stateful session bean class can be invoked by enterprise bean client
checked and uncheked exception
checked and uncheked exception  please give some information about checked and unchecked exception
how to set the values in jsp
how to set the values in jsp  how to set the values text boxs in jsp frm dbase via servlet
Question on Checked Exception
Question on Checked Exception   why checked exception force to put try and catch block ? Please send me answer
how to get only checked data
button it should redirect only checked data to other jsp page which is connected...how to get only checked data  my problem is that i have a jsp page on which i retrieve data from the database through servlet , on the jsp page
where are program instructions and data values stored
where are program instructions and data values stored  Where are program instructions and data values stored in Computer?   In Storage. Actually, whichever program and data we use in our computers are stored in Storage
Match the correct description about purpose and function to which session bean type they apply: stateless, stateful, or both.
session bean type they apply: stateless, stateful, or both. Prev Chapter 3. Session Bean Component Contract Next  ... to which session bean type they apply: stateless, stateful, or both
How to access session values through Ajax?
How to access session values through Ajax?  Suppose in a servlet a variable userName is kept in session. How can I access this variable from JSP through AJAX? Is it possible
Why the null values are stored in Database when I am sending proper values?
Why the null values are stored in Database when I am sending proper values... but the other details are not stored in the Database. Instead of other values, there null values are stored in the Database. Please guide me. Ask any more details
edit values of database using jsp
edit values of database using jsp  hi i want a code to edit the row from tye database and display in a page which containd radio buttons and drop down boxes using jsp code
edit values of database using jsp
edit values of database using jsp  hi i want a code to edit the row from tye database and display in a page which containd radio buttons and drop down boxes using jsp code
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
Double-checked locking,java tutorials,java tutorial
Double-checked Locking Lets us have a look of Singleton pattern, In Singleton... create an instance, remember it is not checked at that time. Therefore we prefer Double-Checked locking. In double checked locking we do something like
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
Passing Parameter Values to another jsp in Query Strings
Passing Parameter Values to another jsp in Query Strings  HI ALL, I m trying to pass a variable value to another JSP using query string... even though some values* stored in the variable loginid. Can any one suggest
how to insert values from jsp into ms access
how to insert values from jsp into ms access   how to insert values using jsp into ms access database
How to get a values - JSP-Servlet
When i submit a form ,inother jsp i.e newMESWithUpload.jsp in that jsp if i do strSubject=request.getParameter("subject... getting a null value aprt from a attachmented file,so how to get a other values
deffernce between checked and unchecked exception in java
deffernce between checked and unchecked exception in java  What is deffernce between checked and unchecked exception in java please explain by example
supplying values to in parameters of stored procedure in ms sql and displaying out parameters
supplying values to in parameters of stored procedure in ms sql and displaying out parameters  I have to execute following procedure ALTER PROCEDURE [dbo].[get_date] @codeId int, @vardate as datetime OUTPUT AS SELECT
How to pass multiple values from a servlet to JSP?
How to pass multiple values from a servlet to JSP?  hi, I want to pass multiple values form a servlet to JSP. Hw do i do that? I am able to send one...) values
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB  The below.... so if I have 5 values in the dropdown this selected one is added as 6th values
How to carry multiple values from a Servlet to a JSP?
How to carry multiple values from a Servlet to a JSP?  By using the below code I am able to carry the username to a JSP (single value). ----response.sendRedirect("index.jsp?uname="+username);---- But I want multiple values
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB
Dynamically display values in dropdown box and then show the selected values as selected by the user which is already stored in the DB   Hello, This is for Updating. I had two SQL queries one for only selected values
validation before entering values into database in jsp
validation before entering values into database in jsp  Hi, my project involves entering data into database from jsp form.but before entering into database it should pop up a alert message that "you have selected product:name
Is session depend on cookie ???
Is session depend on cookie ???  Since I created one session & as we say that session store at server side that means if I clear browser cookie... also give example as when should I use session & when should I use cookie
Inserting values into a database table of selected DropDown in jsp.
Inserting values into a database table of selected DropDown in jsp.  http://www.roseindia.net/answers/viewqa/Ajax/15250-DropDown-in-ajax+jsp.html... to insert all selected values in data base correspondent to each like if user

Ads