• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing HttpSession when using POST

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am learning how sessions work in the browser by using a very simple shopping cart application. I have a button that calls the following servlet which then adds an item (a simple string from a textfield on the webpage) to the shopping cart:


I then forward to a page that tells me what item I have just added. I then click the back button on the browser and can add another item.
My problem is that I have another button (on the same page where I can add items) to display all the items in the cart. This button takes me to another page but it only displays the last item I entered. Say I enter crisps, chocolate and fudge, fudge will only show up. Here is the code for that servlet:



Since I can read the last item I don't know if I am re-instantiating the shopping cart every time I call the servlet that adds the item. The shopping carts 'addItem' method simply adds a 'shoppingCartItem' (which has a String variable called name) to an ArrayList of items in the shopping cart. What am I doing wrong here?

Thanks,
Alan
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems, every time you pass below code, it just overridden.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, personally I dont use the back button of the browser while doing this kind of stuff. And yes I think you are re-instantiating the cart every time.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swetha Popuri wrote:Hey, personally I dont use the back button of the browser while doing this kind of stuff.


Yes, but users will likely do so and the web app met be tolerant of this and continue to work correctly.
 
Swetha Popuri
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I understand.
 
Alan Smith
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Swetha Popuri wrote:Hey, personally I dont use the back button of the browser while doing this kind of stuff.


Yes, but users will likely do so and the web app met be tolerant of this and continue to work correctly.



Hi Bear, I would always use the back button by instinct so how do I get around the problem of reinstantiating the shopping cart? If each user is to have their own cart I can't put it in the application scope. Am I at least on the right track here? Thanks.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The session is the correct place to put the cart. But you need to be sure to get it from the session on each request and only create it once.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Have you read this article? It outlines proper application flow.
 
Alan Smith
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:P.S. Have you read this article? It outlines proper application flow.


No, but I will thanks. So where should I instantiate the cart if not in the session? Would it be possible to check if the cart exists in the session first and if not then create it ie. create only on the first request?
 
Swetha Popuri
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way is if it is the first request, the attribute 'cart' is null in the session.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Smith wrote:
So where should I instantiate the cart if not in the session?


I said the exact opposite. The session is the appropriate place to put the cart in order to have a unique cart for each user, and for it to persist across requests.

Would it be possible to check if the cart exists in the session first and if not then create it ie. create only on the first request?


Exactly.
 
Greenhorn
Posts: 6
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instantiate the ShoppingCart when a new session for user is created.


This can be done by implementing HttpSessionListener....and then you can instantiate your cart in it's

//Overriden Method
public void sessionCreated(HttpSessionEvent event){

ShoppingCart cart = new ShoppingCart();

even.getSession().setAttribute("cart",cart);

}
 
Ankush Puri
Greenhorn
Posts: 6
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing is init() method is called only once in Servlet's lifecycle and that too before the servlet is ready to serve user's request.

This method is called by the Container say for eg. tomcat .

Then when a request for this servlet comes in....container either creates a new thread for this servlet or picks one from thread pool if available...and calls it doServiceMethod() which in turn calls doGet() or doPost()
depending on the type of request.

So if you want to check the names of threads serving your request try printing those in either doGet() or doPost().

Hope this helps.

 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic