Search code examples
jspservletsjakarta-eeshopping-carthttpsession

is using a session is the best way of implementing a shopping cart in java?


I want to implement a shopping cart in my upcoming java project(i will be using your basic JSPs and servlets)

The first thing that came to my mind is to store the shopping cart elements in the user session (httpsession), but I think it is poor to do so because the shopping cart will not be shared across multiple browsers(that mean if I started my shopping using firefox ,closed it then logged in using chrome i will not be able to retrieve my cart .

My questions are , is the assumption above correct ? and to achieve this would i need to persist the shopping cart elements manually to some store and upon login I inject it to the newly created session ? is there a way to do this automatically ?


Solution

  • If you want to persist your shopping cart so it stays alive between logins and even in different browsers then a serverside store is really the only way to go.

    Client side frameworks might be able to store the shopping cart in local storage but a) that doesn't work between different browsers and b) that can still be lost if the user cleans the storage.

    For your 'datastore' you could use a database, perhaps using something like hibernate or eclipselink to automate interaction, or you could implement a filestore or even an in-memory clustered store such as redis. It depends on your requirements.

    Really, you should get more information on what you want to achieve, maybe read a book on web application design for the basics. As it is your question is really a bit too broad.