session array using jsp

Posted on 2006-04-15
Medium Priority
Last Modified: 2008-01-09
I want to have a shopping cart on my website, using JSP language. How do I implement this via a session array?
I need the exact code as I am just learning.

Many thanks
Question by:kgpretty

Author Comment

ID: 16466379
Problem solved, I used a bean:

package com.weselldvds;

public class cart{
  private String[] items = new String[50];
  private int[] quantities = new int[50];

  public void setItemsArray(String itemToAdd)
   if(itemToAdd == null) return;
   for(int i = 0; i < items.length; i++){
   if (items[i] == null){
   items[i] = itemToAdd; break;
  public void removeItem(int i){quantities[i] = 0;}
  public String[] getDvds(){ return items; }

  public int[] getQuantity(){ return quantities; }

This was placed in the web page:

<% String dvd = request.getParameter("dvdId");%>
<% out.println(dvd);%>

<jsp:useBean id="cart" class="com.weselldvds.cart" scope="session"/>
<jsp:setProperty name="cart" property="itemsArray" value="<%=dvd%>"/>

String[] cartItems = cart.getDvds();
for (int i = 0; i < cartItems.length; i++)
    if(cartItems[i] == null) break;
%> <br><%

Accepted Solution

GranMod earned 0 total points
ID: 16469502
Closed, 70 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question