stu
stu

Reputation: 8805

How can I close the output stream after a jsp has been included

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?

Upvotes: 0

Views: 1578

Answers (1)

ZZ Coder
ZZ Coder

Reputation: 75466

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();

Upvotes: 2

Related Questions