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?
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.
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?
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.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL