File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Storing HttpSession when using POST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Storing HttpSession when using POST" Watch "Storing HttpSession when using POST" New topic
Author

Storing HttpSession when using POST

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Jan 28, 2008
Posts: 5575

Seems, every time you pass below code, it just overridden.
Swetha Popuri
Greenhorn

Joined: Jul 19, 2012
Posts: 7
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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Swetha Popuri
Greenhorn

Joined: Jul 19, 2012
Posts: 7
Yeah I understand.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Jan 10, 2002
Posts: 61095
    
  66

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

Joined: Jan 10, 2002
Posts: 61095
    
  66

P.S. Have you read this article? It outlines proper application flow.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Jul 19, 2012
Posts: 7
One way is if it is the first request, the attribute 'cart' is null in the session.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

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

Joined: Jul 16, 2010
Posts: 6

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

Joined: Jul 16, 2010
Posts: 6

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.

 
 
subject: Storing HttpSession when using POST