1

I would like to ask a theoritical question about how some web sites work. As an example.Let's say that I'm in the A market on line store. I placed a case of wine in the shopping trolley, The page appeared with this URL:

www.A.co.uk/webstore/basket.asp?calledby=normal&ProductCode=6379044

I continued shopping and then placed a different wine in the trolley and again the page appeared with this URL

www.A.co.uk/webstore/basket.asp?calledby=normal&ProductCode=6323456

I then Clicked the Back Button on the browser three times and the trolley page appeared again. This time contained ONLY the first item and NOT the second.

In another website I showed the following:

I selected a case of wine. As a result the form containing the wine was posted to this ASP page basket.asp?Item=3605681, where Item is the ID of the particular case of wine. However the page appeared in the browser had a different URL:

www.B.com/extra/basket.aspx?acstore=10&ba=0

I then added another case of different wine to basket. The address that appeared was exactly same as previous one.

When I clicked the Back Button The Shopping basket always showed that I have two items in the basket. How do you think that these Online stores have programmed the site so that shopping basket always shows its current state even if the user presses the Back Button several times? Also, what's the difference of these two situations?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
dali1985
  • 3,263
  • 13
  • 49
  • 68

1 Answers1

1

The basket is likely stored in the session. More than often the session is in turn backed by cookies. In JSP/Servlet it's the JSESSIONID cookie. To test it yourself, locate the cookie in browser's cookie store and delete it. You'll see that a page refresh will result in an empty basket. For more detailed background information, please read How do servlets work? Instantiation, sessions, shared variables and multithreading.

In JSP/Servlet terms, the basket could be retrieved/precreated as follows:

Basket basket = (Basket) session.getAttribute("basket");

if (basket == null) {
    basket = new Basket();
    session.setAttribute("basket", basket);
}

// ...

This lives then as long as the user is interacting with the same webpage within the same session. Any products could be added to the basket as follows:

String productCode = request.getParameter("productCode");
Product product = someProductService.find(productCode);

if (product != null) {
    basket.addProduct(product);
}

// ...

In JSP you could then display it as follows:

<table>
  <c:forEach items="${basket.products}" var="product">
     <tr>
        <td>${product.code}</td>
        <td>${product.description}</td>
        <td>${product.quantity}</td>
        <td>${product.price}</td>
     </tr>
  </c:forEach>
</table>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555