• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why my servlet is not answering when I try to send an object?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello people!

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.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you doing the "sending"? If it's through HTTP, that is a text-only protocol.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is JFreeChart a Serializable class?

If not, the server threw an exception and your applet got an error message in HTML.

I would certainly check the logs.

Bill
 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do like this:

then:



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?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A half second should make no difference, the applet will wait quite a long time and if it does close the connection you will get a error message in the logs.

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.

Bill
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricardo posted his code in this topic.
You don't need to keep pinging your servlet to keep the connection alive. The Java VM takes care of that.
 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum. I'm I was suspecting that I don't need to keep sending packets to keep my connection alive because when I have a servlet to create a image from the same thing, it works perfectly.

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.
 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just tried this new solution without success. Even if I send the parameters via text, I can't get the object from the servlet.
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 :/ )
Servlet


Applet
 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm 100% sure. The problem is that my graph is taking to long to be generated, making my server close its connections(or the applet, I'm not sure who is actually closing it). I'll keep my investigations.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your applet code, when you build the query string, you should use the URLEncoder class to encode your parameter names and values. This makes them safe for inclusion in a URL. For example, it will replace all spaces with "+".


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.
 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum.

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
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only encode the data.
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Ricardo Prado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well. I've solve the problem by not using maven anymore. I have no idea why it solved my problem, but it did.

I'm refactoring my code to do what you guys said.

Thanks for your help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic