Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Copying session data into vector in java

Hi All,

I was trying to copy the cart session data into the vector in java. Actually my session holds the object of ShoppingCart class and now I want to hold contents of session to append the new purchase into the session. Can someone plz help me with this. Here is the code snippet...

HttpSession session = (HttpSession) request.getSession();
        ShoppingCart previousItems = (ShoppingCart)session.getAttribute("cart");
        PrintWriter out=response.getWriter();
        if(previousItems==null)
        {
              previousItems = new ShoppingCart();
              previousItems = (ShoppingCart)session.getAttribute("temp_cart");
              session.setAttribute("cart", previousItems);
        }
        else
        {
              Vector items = (Vector)session.getAttribute("cart");
              out.println(items.capacity());
              out.println(items.size());
        }
        out.println("pid" + previousItems.getProd_id());
        out.println("qty" + previousItems.getQuantity());
        out.println("price" + previousItems.getPrice());
        out.println("category" + previousItems.getCategory());
        out.println("Description" + previousItems.getDescription());
        out.println("<br>");
0
RohitGupta2008
Asked:
RohitGupta2008
  • 2
  • 2
1 Solution
 
wwnosalCommented:
Hi,
I think your problem is that you're trying to cast something that is not a Vector to a Vector.

So your code should look like the one in a snippet.
Please consider storing a vector back into session in your else clause so it is persistent (of course then first part of the if should be modified as well)


HttpSession session = (HttpSession) request.getSession();
        ShoppingCart previousItems = (ShoppingCart)session.getAttribute("cart");
        PrintWriter out=response.getWriter();
        if(previousItems==null)
        {
              previousItems = new ShoppingCart();
              previousItems = (ShoppingCart)session.getAttribute("temp_cart");
              session.setAttribute("cart", previousItems);
        }
        else
        {
              Vector items = new Vector();
              items.add(session.getAttribute("cart"));
              out.println(items.capacity());
              out.println(items.size());
        }
        out.println("pid" + previousItems.getProd_id());
        out.println("qty" + previousItems.getQuantity());
        out.println("price" + previousItems.getPrice());
        out.println("category" + previousItems.getCategory());
        out.println("Description" + previousItems.getDescription());
        out.println("<br>");

Open in new window

0
 
wwnosalCommented:
One more thing: if your previousItems is null you will have a lot of NullPointerException in all of your prints at the end.
0
 
RohitGupta2008Author Commented:
I tried to do this. But I got warning saying
"Type safety: The method add(Object) belongs to the raw type Vector. References to generic type
 Vector<E> should be parameterized"

then wt i did is, parametrized the Vector and the warning gone but problem is still the same. I am getting the first product I add into the cart but unable to hold the cart contents smwhere so that I can append the whole (old+new) into the session. Here is wt i did again.



HttpSession session = (HttpSession) request.getSession();
        ShoppingCart previousItems = (ShoppingCart)session.getAttribute("cart");
        PrintWriter out=response.getWriter();
        if(previousItems==null)
        {
        	previousItems = new ShoppingCart();
        	previousItems = (ShoppingCart)session.getAttribute("temp_cart");
        	session.setAttribute("cart", previousItems);
        	out.println("pid" + previousItems.getProd_id());
            out.println("qty" + previousItems.getQuantity());
            out.println("price" + previousItems.getPrice());
            out.println("category" + previousItems.getCategory());
            out.println("Description" + previousItems.getDescription());
            out.println("<br>");
        }
        else
        {
        	Vector<ShoppingCart> items = new Vector<ShoppingCart>();
        	items.add((ShoppingCart)session.getAttribute("cart"));
        	out.println("capacity" + items.capacity());
        	out.println("size" + items.size());
        	out.println("Session copying started");
        	items.add((ShoppingCart)session.getAttribute("temp_cart"));
        	out.println("Session copied successfully");        	
        }
        

Open in new window

0
 
phuphaxCommented:
Why don't you just keep Vector Object in session?
        HttpSession session = (HttpSession) request.getSession();
        Vector items = (Vector)session.getAttribute("cart");
        PrintWriter out=response.getWriter();
        if(items==null)
        {
              items = new Vector();
              session.setAttribute("cart", items);
        }
        out.println(items.capacity());
        out.println(items.size());
        for(int i=0;i<items.size();i++){
                ShoppingCart previousItems=(ShoppingCart)items.elmentAt(i);
                out.println("pid" + previousItems.getProd_id());
                out.println("qty" + previousItems.getQuantity());
                out.println("price" + previousItems.getPrice());
                out.println("category" + previousItems.getCategory());
                out.println("Description" + previousItems.getDescription());
                out.println("<br>");
        }
 
 
TO INSERT NEW ITEM
 
        HttpSession session = (HttpSession) request.getSession();
        Vector items = (Vector)session.getAttribute("cart");
        PrintWriter out=response.getWriter();
        if(items==null)
        {
              items = new Vector();
              session.setAttribute("cart", items);
        }
        ShoppingCart NEWITEM=......;
        items.addElement(NEWITEM);

Open in new window

0
 
RohitGupta2008Author Commented:
Thanks a lot to all who helped me to get out of the problem...last comment really works for me...thanks again
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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