tags:

views:

27

answers:

1

I have a webpage that makes an ajax call to get some data. That data takes a long time to calculate, so what I did was the first ajax server call returns "loading..." and then the thread goes on to calculate the data and store it in a cache. meanwhile the client javascript checks back every few seconds with another ajax call to see if the cache has been loaded yet.

Here's my problem, and it might not be a problem. After the initial ajax to the server call, I do a

...getServletContext().getRequestDispatcher(jsppath).include(request, response);

then I use that thread to do the calculations. I don't mind tying up the webserver thread with this, but I want the browser to get the response and not wait for the server to close the socket.

I can't tell if the server is closing the socket after the include, but I'm guessing it's not.

So how can I forcibly close the stream after I've written out my response, before starting my long calculations?

I tried

o = response.getOutputStream();
o.close();

but I get an illegal state exception saying that the output stream has already been gotten (presumably by the jsp I'm including)

So my qestions:

  1. is the webserver closing the socket (I'm guessing not, because you could always include another jsp)

  2. if it is as I assume not closing the socket, how do I do that?

+2  A: 

I don't see how you can even close the OutputStream if the calculation is still going on in the same thread.

You need to start the calculation in a new thread and just return from the Servlet and the response will be sent back automatically.

If you really need to close the writer, do this,

  response.getWriter().close();
ZZ Coder
GENIUS. I hope it's doing what I think it's doing. :-)I can close the outputstream because the results of the calculation is stored in a database and that webrequest has no more data to output, so why should the browser wait...
stu
So who is doing the calculation? If this thread is not doing calculation, you don't need to close. Just return and the servlet will handle the close for you.
ZZ Coder