I have a Applet communicating with a servlet. I first send and object from the applet to the servlet without problems, but the problem is when I try to send an object back from my servlet to my applet.
When I try to send an String, its OK (it gives me a ClassCastException error at the Applet side, since I'm waiting for another kind of object) , but when I try to send the object that I really need, it doesn't work. Is there any size limitation? I'm trying to send a JFreeChart object to my applet.
I noticed that if I send an object that is created almost instantly the object is sent correctly, the problem seems to happen when my object takes more than 1/2 seconds to be created. This is huge problem for me since server need to process more than 15.000.000(yes, a lot) samples of data to generate the graph (sometimes it takes more than 10 seconds).
edit: Yes, JFreeChart is serializable!
I think that I'll need to configure something to allow my applet to wait a little more for the answer. Can that be right?
Do you flush/close the object output stream? One cause of apparently no answer is failure to close the output stream.
It might be interesting to write the object to a byte array output stream, then write the byte so you could plug the size into the content-length header first.
Of course, an alternative would be to initiate the JFreeChart creation with one request, then wait a while before requesting the object.
What I'm going to do now is:
Stop sending an object to my servlet. Instead I'll send send the parameters using "text mode", and handle it at server side.
As soon as it works, I'll try come back here to post my results.
Thank you SO MUCH for this help.
By the way, that other code is not "real". After I run it for the first time I realized that it will never work.
I think that the major thing here to be understood is why me servlet accepts small objects but not a bigger one. Why when I send a "empty" chart or even a String it works perfectly?
There is something that is hidden here. Well, I'll post my code here (The system outs is because I can't debug my program anymore due to a NetBeans bug :/ )
Also, the "operadoras" parameter in your applet could cause problems. It looks like that parameter can be very very long? There is a limit to how long a URL can be, so if the URL is too long, you will get back an HTTP error. You might be better off sending a POST request instead (right now, you are sending a GET request). POST requests store the parameters in the body of the request instead of the URL, so that means there's no limit to how long the parameters can be.
The whole idea of sending serialized objects over HTTP smells bad to me. I think that it's inefficient because you are sending not just whatever core data your servlet is generating, but also all the overhead data that comes with the Java object. And like you said, the servlet has to spend time generating the object, which is a big problem in your case. It also breaks the neutrality of HTTP because you are relying on the fact that your client and server are both running Java. My first thought is to get rid of the serialized objects altogether.
I've tried to use this URLEncoder but it also encode my "&" sign (well, I could encode the parameters then concatenate it).
The overhead is very very small. The size of the final object is only 25kb in the worst case. The strange thing is that is works in another project(the SAME CLASSES) but not on this one! And server is also the same! O.o
Ricardo Prado wrote:I've tried to use this URLEncoder but it also encode my "&" sign (well, I could encode the parameters then concatenate it).
Only use URLEncoder to encode the parameter names and values. The special query string characters (?, &, =) should not be encoded. You should do this because what if one of your parameter values had a "&" in it? It would completely mess up the query string. URLEncoder escapes these values.
Ricardo Prado wrote:The strange thing is that is works in another project(the SAME CLASSES) but not on this one! And server is also the same! O.o
Are you using the same version of the JFreeChart JAR in both projects?
Did you try William's suggestion? There might be an issue with serializing the object directly to the response. I don't think that manually setting the Content-Length header is necessary, but you should try it anyway.
William Brogden wrote:It might be interesting to write the object to a byte array output stream, then write the byte so you could plug the size into the content-length header first.
I'm refactoring my code to do what you guys said.
Thanks for your help!