Shopping cart persistence
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
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
-
VimleshMishraJohn, create your own HttpSessionListener and override sessionCreated/sessionDestroyed method to provide logic.
-
micheal johnThanks for your reply #-Link-Snipped-#
i'm getting session values inside sessionDestroyed(), but calling getHibernateTemplate().save(orders); inside sessionDestroyed() throws "java.lang.NullPointerException" -
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].
-
micheal john
Thanks it worked😀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]. -
micheal john#-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
😔 -
VimleshMishraDid 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 johnonce user adds item into cart i call addItem and set session attribute
cartService.addItem(item);
req.getSession().setAttribute("cartService", cartService); -
VimleshMishraI 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 johnpublic 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- 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());
}
} - cartList = cartService.getAllItems();
-
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.
-
micheal john
OkVimleshMishraHey 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.
You are reading an archived discussion.
Related Posts
I just happen to find out that Android KitKat 4.4.2 allowing camera even a Point & Shoot to connect with cable directly and import your photos to phone without any...
I hope you must have noticed this little addition to CrazyEngineers that makes the whole process of starting a new discussion or posting a new idea extremely easy. No matter...
I was reading an article in the Economic Times that Indian software major Infosys is considering hiring an Israeli consultancy firm to help boost its revenues by removing the bottlenecks...
Developed by Engineered Arts Limited, SociBot-Mini is a one-of-its-kind robot that has a fully integrated LED pico projection technology with custom developed optics. In simple words, the robot features a...
hi people! I hold a BTECH degree in mechanical engg. im working in L&T currently. im looking to do MS in aerospace engg. can someone suggest gd colleges. my GPA...