session array using jsp

Posted on 2006-04-15
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

    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

    Closed, 70 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now