Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList in servlet with previous values.  RSS feed

 
kiran Gouda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I declared an arraylist in doPost method and i passed it to the another jsp. The problem is arraylist contains old values for every new request.
Thanks in advance..
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you pass the arraylist to the JSP? Are new requests going to the servlet all the time? Are you adding differnt values to the arraylist everytime? You'll need to provide more details and maybe a bit of code to help those that might be able to help you.
 
kiran Gouda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for wrong information. I am sending the arraylist to another servlet

Yes new every new request will go to the servlet.

Here i am providing the code

public void doPost(HttpServletRequest reqst,HttpServletResponse res){

ArrayList<String> tabledata=new ArrayList<String>();

Calculation(tabledata); //Which adds some strings to the tabledata

HttpSession hs=reqst.getSession();

hs.setAttribute("tabledata",tabledata);

res.sendRedirect("nextpage")// redirecting to another servlet page
}

i want the arraylist to contain only present request data.but it is storing all the previous request's data also.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i want the arraylist to contain only present request data.but it is storing all the previous request's data also.

Can you be more precise? It will help greatly if you can provide an example.
and another thing, i can see in your code

Where do you declare this function? Can you also post it?
 
kiran Gouda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a starting servlet from there i redirected to this servlet. Now for the first time tabledata size and Bufferdata size is 8. If i refresh the first servlet it is again redirecting to this servlet(i wanted it) . but the sizes are becoming 16,16 which mean previous request data is also stored in the arraylist. Until I restart the tomcat server for every new request arraylist size is increasing by 8.
In the net i read that for every new request to server , it will create a new thread. If the variables are local then a new variable will be created for each thread. In my code arraylist are local so they should be created for each request and their size should be 8, instead of that same arraylist is being used for every request so the size is increasing every time by 8.

is my arraylist not locally declared?



 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted earlier was creating a new List and adding it into session.
The code you have posted now gets the list from the session and doesn't add anything into the session.
You are either not posting the exact code you are using.
I suspect you are storing the list in session and updating that same list all the time. If the list is in session scope then it will be the same list for all new requests which is why you are seeing the old values in it. I suggest you read about the differences between scopes.
You should also use standard and consistent naming conventions for your variables, methods and classes.

not only uses non standard naming conventions but is also inconsistent with itself.






 
kiran Gouda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. Your are right i am storing the list in session and updating that same list all the time.
I thought adding list into session won't create any problem and it will be readable for you so i skipped that code sorry. And thank you for your suggestions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!