0

I declare List in Servlet for temporary hold the data, before inserting database:

List poList= new ArrayList(); 

First time user enter the data,and then after 10-15 or 30-45 minutes he enter more data.

Does old data is available in Servlet or Servlet Destroy the all previous entered data after prescribed time ?

Both FM
  • 770
  • 1
  • 14
  • 33
  • Where in the servlet is `poList` declared? Is it a local variable within a method or is it a member variable of your servlet class? – QuantumMechanic May 28 '12 at 18:49
  • It a member variable of my servlet class! @QuantumMechanic – Both FM May 28 '12 at 18:53
  • 1
    Learn here how servlets work: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC May 28 '12 at 18:59

3 Answers3

2

Don't do that. There is only one servlet instance for all the users of this webapp. This means that all the users will store data, concurrently, in the same list. The HTTP session is the place where to store data associated to one particular user, and whose lifetime must span several requests.

First request:

request.getSession().setAttribute("myList", list);

Second request:

List<Something> myList = (List<Something>) request.getSession().getAttribute("myList);

Also note that destroy doesn't matter: destroy is called when the application ends (because the server is stopped, for example).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

If you want to store the data per user, this wont serve your purpose.Add the poList to the HttpSession as attribute.Manipulate it in your doPost()/service() method.

Ahamed Mustafa M
  • 3,069
  • 1
  • 24
  • 34
0

It depends. If your poList is in a singleton or is declared as an static List, values are keep on your poList.

Guillaume USE
  • 436
  • 1
  • 4
  • 6