CrazyEngineers
  • 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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • VimleshMishra

    MemberMar 30, 2014

    John, create your own HttpSessionListener and override sessionCreated/sessionDestroyed method to provide logic.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 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"
    Are you sure? This action cannot be undone.
    Cancel
  • VimleshMishra

    MemberApr 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].
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 1, 2014

    VimleshMishra
    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].
    Thanks it worked😀
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 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
    😔
    Are you sure? This action cannot be undone.
    Cancel
  • VimleshMishra

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 10, 2014

    once user adds item into cart i call addItem and set session attribute
    cartService.addItem(item);
    req.getSession().setAttribute("cartService", cartService);
    Are you sure? This action cannot be undone.
    Cancel
  • VimleshMishra

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 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());
    }

    }
    Are you sure? This action cannot be undone.
    Cancel
  • VimleshMishra

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • micheal john

    MemberApr 10, 2014

    VimleshMishra
    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.
    Ok
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register