Forums Register Login

ArrayList in servlet with previous values.

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 


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?
+Pie Number of slices to send: Send
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?



+Pie Number of slices to send: Send
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.






+Pie Number of slices to send: Send
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.
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2636 times.
Similar Threads
Usage of indexOf method of ArrayList class
How to print the elements of an arraylist that contains a hashmap
Datatype casting. Split Strings to double 2D array. (loss of precision)
NX: how to fill long[] results in findByCriteria()
Calculate the average of an ArrayList
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:44:14.