aspose file tools*
The moose likes Servlets and the fly likes Why my servlet is not answering when I try to send an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why my servlet is not answering when I try to send an object?" Watch "Why my servlet is not answering when I try to send an object?" New topic
Author

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

Ricardo Prado
Greenhorn

Joined: Oct 01, 2010
Posts: 13
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.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61627
    
  67

How are you doing the "sending"? If it's through HTTP, that is a text-only protocol.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
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

Joined: Oct 01, 2010
Posts: 13
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
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
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
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

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.


[How To Ask Questions On JavaRanch]
Ricardo Prado
Greenhorn

Joined: Oct 01, 2010
Posts: 13
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

Joined: Oct 01, 2010
Posts: 13
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

Joined: Oct 01, 2010
Posts: 13
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.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
Ricardo Prado
Greenhorn

Joined: Oct 01, 2010
Posts: 13
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61627
    
  67

Only encode the data.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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

Joined: Oct 01, 2010
Posts: 13
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why my servlet is not answering when I try to send an object?