Shopping cart persistence

micheal john

micheal john

@micheal-john-l1fIn3 β€’ Oct 26, 2024

Hello folks,
Need to write a web app (Using SpringMVC) where users can search for particular item and add an item to cart then purchase it. But if user decided to purchase item later i need to save a users shopping items to database, is it possible to save cart items to session and just before session timeout can i save a users cart items into db.

i have implemented session driven shopping cart, but needed a database driven shopping cart.
i'm confused πŸ˜”
please help

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • VimleshMishra

    VimleshMishra

    @vimleshmishra-rWc8Yv Mar 30, 2014

    John, create your own HttpSessionListener and override sessionCreated/sessionDestroyed method to provide logic.

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 1, 2014

    Thanks for your reply #-Link-Snipped-#
    i'm getting session values inside sessionDestroyed(), but calling getHibernateTemplate().save(orders); inside sessionDestroyed() throws "java.lang.NullPointerException"

  • VimleshMishra

    VimleshMishra

    @vimleshmishra-rWc8Yv Apr 1, 2014

    I believe that getHibernateTemplate() method returning you null [please debug and verify] if yes it because of you are not in spring context inside HttpSessionListener. ServiceLocator Pattern will help you here [you have to get reference of HibernateTemplate object from already initialized spring context].

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 1, 2014

    VimleshMishraI believe that getHibernateTemplate() method returning you null [please debug and verify] if yes it because of you are not in spring context inside HttpSessionListener. ServiceLocator Pattern will help you here [you have to get reference of HibernateTemplate object from already initialized spring context].

    Thanks it workedπŸ˜€

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 9, 2014

    #-Link-Snipped-#, now i facing a different problem that is ,
    a user login and add items to shopping cart, afterwards click browser back button until he reaches login page and now login with different username and password and clicks on shopping cart now he sees previous users item in his list.

    session is getting destroyed, but shopping cart items remains

    please help
    πŸ˜”

  • VimleshMishra

    VimleshMishra

    @vimleshmishra-rWc8Yv Apr 10, 2014

    Did you debug the code? How are you storing items of shopping cart in session? Debug sessionCreated and sessionDestroyed method and try to find how earlier items were added in session, let me now your findings.

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 10, 2014

    once user adds item into cart i call addItem and set session attribute
    cartService.addItem(item);
    req.getSession().setAttribute("cartService", cartService);

  • VimleshMishra

    VimleshMishra

    @vimleshmishra-rWc8Yv Apr 10, 2014

    I don't think problem is here, problem must be in session listener which populating session with existing items. Debug it or will be great if you can share source code of your session listener.

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 10, 2014

    public class MySessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent event) {
    System.out.println("sessionCreated()");
    System.out.println("Session created ID --> " + event.getSession().getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
    synchronized (this) {
    try
    {
    System.out.println("sessionDestroyed()");
    ServletContext servletContext = event.getSession().getServletContext();
    WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    RegistrationDAO registrationDAO = (RegistrationDAO) appContext.getBean("registrationDAO");

    Cart cartService = (Cart) event.getSession().getAttribute("cartService");
    User user = (User) event.getSession().getAttribute("userSession");
    int ShoppingcartSerialNum = 0;
    Date dat = new Date();
    String date = ""+ (dat.getDate() + dat.getDay() + dat.getYear() + dat.getTime());
    String ShoppingcartRefNum = date + user.getUserId();
    if(cartService != null && !user.getEmailId().equals("#-Link-Snipped-#") && user != null)
    {
    List<Item> cartList = cartService.getAllItems();
    for (Item item : cartList) {
    if(item.getShoppingCartRefNum().equals("0"))
    {
    ++ShoppingcartSerialNum;
    String num;
    if (item.getSearchType().equals("Person")) {
    num = ((uri.external_search_person_asic_gov.ReplyDataType.Person) (item
    .getSearchDetails())).getPersonName().getPersonId();
    }
    else if (item.getSearchType().equals("Business")) {
    num = ((uri.external_bn_search_asic_gov.ReplyDataType.Entities) (item
    .getSearchDetails())).getBusinessName().getNniNumber();
    }else {
    num = item.getNumber();
    }
    registrationDAO.insertorder(item.getDescription(),
    item.getSearchType(), num, item.getExtractType(),
    item.getCost(), user, ShoppingcartSerialNum,
    ShoppingcartRefNum, item.getRequestMessage(),
    item.getCountOfName(), item.getPersonSearchIdfr(),
    item.getPersonIdentifier(), item.getNumber(),
    item.getRegisterType(), item.getWeekStartDate(),
    item.getSummaryType());
    }
    }
    }
    }catch(Exception e){
    // e.printStackTrace();
    System.out.println("Root cause of exception "+e.getCause());
    }

    }
    System.out.println("Session destroyed ID --> " + event.getSession().getId());
    }

    }

  • VimleshMishra

    VimleshMishra

    @vimleshmishra-rWc8Yv Apr 10, 2014

    Hey why no any logic in sessionCreated method? You should set cart items in session once session created. Get items from database for given logged in user and set values in session to show on shopping cart page.

  • micheal john

    micheal john

    @micheal-john-l1fIn3 Apr 10, 2014

    VimleshMishraHey why no any logic in sessionCreated method? You should set cart items in session once session created. Get items from database for given logged in user and set values in session to show on shopping cart page.

    Ok