• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using ArrayList to store values into it

 
John Dale James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sending values from my index.html to ServletOne. On the specified servlet, the values that I have acquired from the index.html are going to be stored through session that I am going to be passing on to ServletTwo, in which the values from the session at ServletOne are going to be stored using an ArrayList. The problem is, I wanted the program to be able to go back to index.html and be able to do everything from the start (index.html), and storing all the inputs, including the previous inputs be stored on the ArrayList on ServletTwo.

This is ServletOne

This is ServletTwo

My speculation on the problem is it actually instantiates a new ArrayList when I go iterate it back to the ServletTwo.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Dale James wrote:My speculation on the problem is it actually instantiates a new ArrayList when I go iterate it back to the ServletTwo.

Well, if it does that via its doGet() method then it definitely will, because you create a new ArrayList every time you run it.

Furthermore:
1. That ArrayList doesn't appear to be used anywhere else, so it will be garbage-collected as soon as the method exits.
2. You only ever appear to add one name to it.

I suspect that 'lNames' should be an instance variable of ServletTwo, but I'm no expert at this stuff.

HIH

Winston
 
John Dale James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Well, if it does that via its doGet() method then it definitely will, because you create a new ArrayList every time you run it.

Right, I know that this might cause a lot of trouble in the future, but declaring the ArrayList as static variable at class level fixes the problem.
So yeah, I guess it's fixed now. But I'm still wondering what the best and the most formal approach would be.
Anyway, thanks for the reply! You guys can mark this as solved.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Dale James wrote:Right, I know that this might cause a lot of trouble in the future, but declaring the ArrayList as static variable at class level fixes the problem.
So yeah, I guess it's fixed now. But I'm still wondering what the best and the most formal approach would be.
Anyway, thanks for the reply! You guys can mark this as solved.


no no no no no no...etc etc.

You don't want to either give a servlet state (that is instance attributes) or have static variables.

Servlets operate in a multi threaded environment so any state is prone to weird and wonderful errors caused by multiple requests mucking about with the values.

Now, what is it you actually want?
If it's storing each entered set of data, then the formal way would be some form of database.
A session isn't intended to survive for longer than, well, the session.

If you want to store a single session's worth of stuff then you could stick the List in the session, rather than the individual values entered. That would also mean you should create a class to represent your data.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just backing Dave up: never, ever, ever, ever, ever, ever, and did I say ever create read/write instance or class variables in a servlet. Just don't do it. You will appreciated the debugging nightmare you save yourself.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic