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

 
 


Zaheer Ramjaun
JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean
0 Answer(s)      a year and a month 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?

View Answers









Related Pages:
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
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
Writing Calculator Stateless Session Bean
' bean. Writing JSP and Web/Ear component Our JSP file access the session bean...Writing Calculator Stateless Session Bean... Bean for multiplying the values entered by user. We will use ant build tool
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
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0
;. A Stateful session bean preserve the information about its content and values...-client multitiered consisting of jsp, servlet and session bean. ... crashes all the data stored in Session Bean are lost. But Entity Beans
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
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
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
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
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
Writing Session bean - Session Bean Example with Source Code
terminates.  Session Bean Types Session Beans are of two types, Stateful... is often called the conversational state. Writing Stateful Session Bean...Writing Session bean In this lesson you will learn how to develop Hello
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
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
Identify the use and the behavior of the ejbPassivate method in a session bean, including the responsibilities of both the container and the bean provider.
responsibilityPassivation is performed only for STATEFUL session beans. The Bean... in a session bean, including the responsibilities of both the container and the bean provider. Prev Chapter 3. Session Bean
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
Chapter 4. Session Bean Life Cycle
the life cycle of a stateful or stateless session bean instance. Stateful Session Bean A session bean... Chapter 4. Session Bean Life CyclePrev Part I. 
using ajax and jsp (struts) to login and remain on same page.
using ajax and jsp (struts) to login and remain on same page.  I am... want to remain on same login page and just want to display loggers name ... I don't know how to use struts to remain on same page. What about config file:- <
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
Stateless Session Bean Example
Stateless Session Bean Example   ... stateless session bean. The purpose of example is to performs the mathematical... bean: The enterprise bean in our example is a stateless session bean called
Stateful Session Beans Example, EJB Tutorial
Stateful Session Bean Example   ... stateful session bean. The purpose of account is to performs two transaction...: The enterprise bean in our example is a statelful session bean called AccountBean
Java bean example in JSP
session. Any JSP page participating in the session can use that bean... Java bean example in JSP     ... the procedure of handling session and print a Hello world using Java Bean. The Bean
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
Activity display dynamically depend on the locale on Struts
Activity display dynamically depend on the locale on Struts  i want... and that depend on the Locale (like English or French) . dynamically means user.... i have an array list in a Java class. In this list i want to store the values
Pass values from form to form
values to different pages. Here we have created three JSP pages. FirstForm.jsp... Pass values from form to form       Java program to pass values from one form to another form
An Entity Bean Example
, such as relational database an entity bean persists across multiple session and can... with annotations that specify how the object should be stored in the database.  The EJB... Implement the Annotated Session Bean: BookCatalogBean
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 write a select box and id should be stored in database?
How to write a select box and id should be stored in database?  Hi...) should be stored in database using SWINGS concept plz help   You... into data1(id,name) values(60,'"+name+"')"); System.out.println("Inserted
want to insert values in drop down menu in struts1.3
:fn="http://java.sun.com/jsp/jstl/functions" xmlns:bean="http://struts.apache.org/tags-bean" version="2.0"> <jsp:directive.page contentType...want to insert values in drop down menu in struts1.3  I am using
Exceptions - More
for illegal user input -- that should have been checked in the interface. This should... be put into two groups: checked exceptions and unchecked exceptions. There is some controversy about which type you should use. A discussion
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 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
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
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
Displaying database values in pdf format
the form the values are stored in database,the database name is registration... database values should be shown as pdf or excel format. Thanks in advance...Displaying database values in pdf format  Hi All, I am
reain values
if it already exits i m using reuest.sendredirect to same jsp but all textbox values are cleard, i want to retain all values in textboxs after request.sendredirect .Please...reain values  Sir I have Jsp page for regisering customers,when i
retrive values - JSP-Servlet
retrive values  how to get multiple values from html to jsp
stateless session bean with methods error - Java Beginners
stateless session bean with methods error  I have to create stateless session bean with 3 methods and then create a servlet which remotely calls all three methods in that session bean. I have 4 files created-index.jsp under web
Get values in drop down list
Get values in drop down list  Pls provide me jsp code to get values... registration .. it ll have company id and company name fields .... these both fields should be automatically inserted in customer registration page and should be shown
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
developing a Session Bean and a Servlet and deploy the web application on JBoss 3.0
a Calculator Stateless Session Bean and call it through JSP file  and deploy... the MyTestSession Session Bean developed in Lesson 3. Infact we will use the same... create our Calculator Session Bean write the deployment descriptor. Writing
From a list, identify the purpose, behavior, and responsibilities of the bean provider for a CMP entity bean, including but not limited to: setEntityContext, unsetEntityContext, ejbC
or initialize the values of any instance variables that depend... that depends on the persistent state of an entity bean should.... The entity Bean Provider should use the ejbStore
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
JSP textbox autopopulation on basis of SQL table values
, then a text box (name - 'responsible') on the same JSP should be populated with 'ABC...JSP textbox autopopulation on basis of SQL table values  Hi, I need... CONTROL NEW ABC LEGAL Dept PENDING PQR There are 2 list box on JSP , one
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.