title: Display the items in the cart in a jsp file from servlet
body: <p>I am building a E-Commerce simple solution in java and i am stucked in a critical point. I am not able to display the correct items. I am using a servlet that has a doGet a shoppingcart class, a products class and viewcart.jsp file. To pass the items between servlets  i am using the setAttribute of context. Here are my files i have build so far. Can anyone help me fix this problems? Every time i press add to car it adds the element but look like all the previous elements get overwritten by the last one i added.</p>
  
  <pre><code>package com.kd.ecommerce;
  
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class addtocart
   */
  @WebServlet("/addtocart")
  public class addtocart extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private shoppingcart shop = new shoppingcart();
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      ServletContext thisContext = getServletContext(); 
      String name = request.getParameter("product");
      Connection cn = DBConnect.getInstance();
      try {
          Statement st = cn.createStatement();
          ResultSet rs = st.executeQuery("select * from inventory where name = '"+name+"' LIMIT 1");
          while(rs.next()){
              Products p = new Products(rs.getString(2),rs.getString(3),rs.getString(4),rs.getFloat(5));
              System.out.println(p.getName()+":"+ p.getPrice());
              shop.ins(p);
              thisContext.setAttribute("shop", shop.getIt());
              for(int i = 0; i&lt; shop.getIt().size(); i++){
                  System.out.println(shop.getIt().get(i)+":"+ shop.getIt().get(i).getPrice());
              }
              response.sendRedirect("inventory.jsp?addedto=success");
          }
      } catch (SQLException e) {
          e.printStackTrace();
      }
  
  }
  
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  }
  
  }
  </code></pre>
  
  <p>Products.java</p>
  
  <pre><code>package com.kd.ecommerce;
  
  public class Products {
  private static String name;
  private static Float price;
  private static String desc;
  private static String img;
  
  public Products(String n, String d, String i,Float p){
      name = n;
      price = p;
      desc = d;
      img = i;
  }
  
  public String getName(){
      return name;
  }
  
  public Float getPrice(){
      return price;
  }
  
  public String getImage(){
      return img;
  }
  
  public String getDesc(){
      return desc;
  }
  }
  </code></pre>
  
  <p>shoppingcart.java</p>
  
  <pre><code>package com.kd.ecommerce;
  
  import java.util.ArrayList;
  
  public class shoppingcart {
  private static ArrayList&lt;Products&gt; items;
  
  public shoppingcart(){
      items = new ArrayList&lt;Products&gt;();
  }
  
  public void ins(Products p){
      items.add(p);
  }
  
  public void remove(Products p){
      items.remove(p);
  }
  
  public int getSize(){
      return items.size();
  }
  
  public ArrayList&lt;Products&gt; getIt(){
      return items;
  }
  }
  </code></pre>
  
  <p>viewcart.jsp</p>
  
  <pre><code>&lt;jsp:include page="menu.jsp"&gt;&lt;/jsp:include&gt;
  &lt;% 
  ServletContext sc = getServletConfig().getServletContext();
  if(sc.getAttribute("shop") != null){
      ArrayList&lt;Products&gt; it = (ArrayList&lt;Products&gt;)sc.getAttribute("shop");
      out.println(it);
      for(int i = 0; i&lt; it.size(); i++){
          out.println("&lt;span class='price'&gt;"+it.get(i).getPrice()+"&lt;/span&gt;");
      }
  }else{
      out.println("&lt;span class='empty'&gt;Shopping cart empty&lt;/span&gt;");
  }
  %&gt;
  </code></pre>
  
  <p>-- EDIT --</p>
  
  <p>My new viewcart.jsp using sessions</p>
  
  <pre><code>&lt;% 
  if(session.getAttribute("shop") != null){
      shoppingcart sh = (shoppingcart)session.getAttribute("shop");
      ArrayList&lt;Products&gt; pd = sh.getIt();
      for(int i = 0; i&lt; pd.size(); i++){
          out.println("&lt;span class='price'&gt;"+pd.get(i).getPrice()+"&lt;/span&gt;");
      }
  }else{
      out.println("&lt;span class='empty'&gt;Shopping cart empty&lt;/span&gt;");
  }
  %&gt;
  </code></pre>