• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing HttpSession when using POST

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems, every time you pass below code, it just overridden.
 
Swetha Popuri
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I understand.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Have you read this article? It outlines proper application flow.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is if it is the first request, the attribute 'cart' is null in the session.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankush Puri
Greenhorn
Posts: 6
Eclipse IDE Java jQuery
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java jQuery
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic