Solved

Copying session data into vector in java

Posted on 2008-10-08
5
465 Views
Last Modified: 2013-11-24
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
Comment
Question by:RohitGupta2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:wwnosal
ID: 22672623
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
 
LVL 6

Expert Comment

by:wwnosal
ID: 22672644
One more thing: if your previousItems is null you will have a lot of NullPointerException in all of your prints at the end.
0
 

Author Comment

by:RohitGupta2008
ID: 22675396
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
 
LVL 2

Accepted Solution

by:
phuphax earned 500 total points
ID: 22675675
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
 

Author Comment

by:RohitGupta2008
ID: 22676007
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

733 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