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









Related Tutorials/Questions & Answers:
values of Combo boxes are not stored in database - JSP-Servlet
values of Combo boxes are not stored in database   i have some combo box values. when i click the submit button after select combo box values, the values are not going in database. please review the code: function
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:ADS
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: <
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: <
Stateful Session Bean Example
Stateful Session Bean Example   ... using stateful session bean. The purpose of account is to performs two... with javax.ejb.Remove in the stateful session bean class can be invoked by enterprise
Get values from session to array
Get values from session to array  Hello I have stored my 2 dimensional array into session using C#.net. Now on aspx page i want to store same session variable into 2 dimensional array. And how to find count of session valaiable
Get values from session to array
Get values from session to array  Hello I have stored my 2 dimensional array into session using C#.net. Now on aspx page i want to store same session variable into 2 dimensional array. And how to find count of session valaiable
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
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
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
retrive values - JSP-Servlet
retrive values  how to get multiple values from html to jsp
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
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
retrive values - JSP-Servlet
retrive values  how to retrive multiple values from html to jsp  Hi Friend, Try the following code: 1)calljsp.html: Enter Name: Enter Address: Gender:MF Qualification: Btech MBA MCA MSC
passing values - JSP-Servlet
passing values  hi this is my jsp page Reserved By: Conference Hall..._DataInsertion(Name,Hallno,Time,Date,Mobileno,Status) values('"+name+"','"+hallno+"','"+time
insert values - JSP-Servlet
insert values  How to insert values in the oracle database using JSP. Plz tell the core answer as soon as possible .  HiThis is html...;insert user_details values('"+username+"','"+jobposition+"
Loading updated values - JSP-Servlet
Loading updated values  In my jsp project profile updating is one of that part. While updating i have to show updated values in choice list for birthday date column. How can we show the previously updated values in choice list
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 get a values - JSP-Servlet
How to get a values  Dear sir, I have one input text field and one submit button .Within one file how to get a values within a same jsp. Thanks and Regards Harini Veerapur.  Hi Friend, Try the following
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... into the synchronized block get create an instance, remember it is not checked at that time. Therefore we prefer Double-Checked locking. In double checked locking we do
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
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
Get values from JSP pages - JSP-Servlet
Get values from JSP pages  I have welcome.jsp in which it has... to another JSP page?Do you want to use database to add column values or you want... Name, City, State) into into next jsp page. note these are not Text fields
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
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
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
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
Java program using stateful session - Java Beginners
Java program using stateful session   Write a program using stateful session bean to add three numbers by accepting input from the user
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
passing values form javascript - JSP-Interview Questions
passing values form javascript  please i want to pass values from javascript function to jsp code how can i do
previous values in servlet - JSP-Servlet
previous values in servlet  Hi... I've an application where there are two links link1 and link2 when i clicked link1 there's a form with relevant... to the link1 after link2 the same values are still in the form... How to remove
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
count values using jsp - JSP-Servlet
count values using jsp  hai deepak i have a query i am dynamically generating textbox with names in that i am having headings... many subheading like that is it posssible with jsp i think it is possible
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
how to store a dynamic values - JSP-Servlet
in that i have stored a values from a excel sheet specified column values and i... and bonus is ~5 . Thanks Hr in another arraylist i have a values ~2,~3,~4,~5 values.Now i want to insert these values into a string ,use that string
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
display checked ckeckbox in same jsp page
display checked ckeckbox in same jsp page  how to display checked... that database, i have displayed all values with checkbox in one table.ADS... jsp page. (for example, as it happens in VB that after clicking checkbox
getting values from database - JSP-Servlet
JSP code separately.If it will not display database values then try your code...getting values from database  I tried the following code abc.html aaa.jsp I am not getting exceptions now
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
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file  Hi am new to java. i need to create... a jsp file which contains a textbox, name issuedescription. when user types
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
how to get a values from processRecord - JSP-Servlet
how to get a values from processRecord  dear sir, i have a problem in getting a values from java to jsp.Here jsp is used to read a excel sheet that intern call a java program so i want a column values i.e a column contains

Ads