I've been working on a project that uses Servlets on the server-side and an Applet on the client-side. The applet contains a JList. When loading, the applet sends a request to a servlet for data from a database. The servlet obtains the necessary records and loads a 2D array. I made a serialized class named TableData which wraps the 2D array. The servlet then sends the TableData object out the wire using the writeObject() method. The applet then receives the TableData object with the readObject() method and extracts the 2D array in order to load the JList.
I rudely discovered that this approach only works if the data being sent consists of only 15 - 20 records from the database. Otherwise, anything greater than 20 records, I wind up with this OutOfMemory Error message. This is not good! I hate these kind of gotchas!
I'm sure there must be many out there who have loaded JLists or JTables with data in applets before. Is there a better approach to this problem than the one I just described that avoids this OutOfMemory error?
Alan
04-02-2005, 04:46 PM
ractoc
I had a similar problem when creating a webpage from a servlet. The problem lies in the fact that you are first contructing the entire object in memory on the server before you are writing to the client. What you should do is start writing to the client as soon as possible. So when you read the first record from the recordstore, imidiately write it to the client. This keeps the server memory clear, since there's no longer any need to store the data there. You might need to rethink you communications protocol a bit though.
04-03-2005, 12:50 PM
ashiers
As it stands now, I'm wrapping the 2D array of data in a class named TableData, and sending the whole data at once to the applet. Are you saying, perhaps, I should send one record at a time to the applet using the same approach? This would mean using a looping structure at each end, I think. Or do you have something else in mind?
Alan
04-03-2005, 12:55 PM
Jamie
I think a looping solution is good, because then the client side will never look stalled, lagged or crashed. So as data comes into the client, update the GUI with the new record.
Think of an extreme case, returning 5000 records (assuming you didn't receive the OutOfMemory Exception) - the client might be waiting a long time for the data to come through.