Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa EJB JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Zaheer Ramjaun
JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean
0 Answer(s)      2 years and 7 months ago
Posted in : EJB


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?


Advertisement
View Answers

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
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
Advertisements
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
checked and uncheked exception
checked and uncheked exception  please give some information about checked and unchecked exception
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
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
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: <
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
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
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
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
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
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
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
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
retrive values - JSP-Servlet
retrive values  how to get multiple values from html to jsp
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
display checked ckeckbox in same jsp page
display checked ckeckbox in same jsp page  how to display checked... have displayed all values with checkbox in one table. Now i want to display ,selected checked checkboxes in another table in same jsp page. (for 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
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+"
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
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
Session Bean
such as making a reservation or validating a credit card, a session bean should be used... to these variables which he wants to persist. A stateful session bean retains its.... If the stateful session bean's state is changed during a method invocation
Stateful Session - Java Server Faces Questions
Stateful Session  HI, I want to have a stateful sessions in JSF, the functionality will be almost similar to the Stateful session of EJB but we are not using any EJB framework. Thanks in Advance  http
Given a list of methods for a stateful or stateless session bean class, define which of the following operations can be performed from each of those methods: SessionContext interface metho
Given a list of methods for a stateful or stateless session bean class... access, and other enterprise bean access. Stateful Session Bean... enterprise bean access.Prev Chapter 4. Session Bean Life Cycle 
Advertisements
 

 

 

DMCA.com