Hello, I am trying to implement a cart.
In Mapu save objects basket. After that, if the order is confirmed everything goes to the database.
Faced the problem that if the goods are added to the cart and enter the site from another computer, the added goods remain.
And actually the question is, how is it better to implement the basket?
Google has very little information on this issue, so I thought it was a bad practice to implement an online store or shopping cart on java?
Here is my code:

@Service("bookStoreService") public class BookStoreService { private Map<Product, Integer> products = new LinkedHashMap<>(); @Transactional public void addProductToBasket(Product product, int count) { products.put(product, count); } } 

Controller

 @RequestMapping(value="/addProduct/{id}",method = RequestMethod.GET) public String addProduct(@PathVariable("id") Integer id){ Product product = bookService.getProductById(id); bookService.addProductToBasket(product, 1); return "redirect:/"; } @RequestMapping(value = "/") public String index(Model model,Model mode1) { model.addAttribute("listProduct",bookService.listPtoduct()); mode1.addAttribute("productInBasket", bookService.getBasket()); return "index"; } 

index.jsp

 <c:if test="${productInBasket.size()>0}"> <table> <tr> <td>Название</td> <td>Цена</td> <td>Кол-во</td> <td><a href="/BookStore/clear">Очистить</a></td> </tr> <c:forEach items="${productInBasket}" var="qwe"> <tr> <td>${qwe.key.name}</td> <td>${qwe.key.price}</td> <td>${qwe.value}</td> </tr> </c:forEach> </table> </c:if> 

    1 answer 1

    This functionality is quite possible to implement in java. But this should be done using cookies . Those. save some information about the user on the client side. The moment he connects to your application, you ask him for this information. This can be any unique identifier. Using it, then upload data about this user, be it a shopping basket, browsing history, etc.
    How it is implemented in spring, it is better to see in the documentation.

    • Thank you, I did. - user256385