• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2030
  • Last Modified:

session array using jsp

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
1 Solution
kgprettyAuthor Commented:
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><%
Closed, 70 points refunded.
The Experts Exchange
Community Support Moderator of all Ages
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now