Launching a new theme in a servlet

When the request reaches the servlet that handles the file upload, it would be nice to start a new thread in this servlet using new Thread(r).start()one that will process the other part of the data that comes with the downloaded file. I wanted this to be done in parallel with the assignments.

+5
source share
3 answers

This is not only a bad idea, but it will not work either. Here's why: your file upload request will end up in a method doPost(). While you are in this method, the container keeps the connection open. As soon as you return from this method (and if you decide to process the incoming data in a separate stream, it doPost()will end earlier), the container assumes that you are done with the request and close the connection. From the point of view of the client, the download was interrupted by the server. And due to the asynchronous nature of the threads, interruption will occur at a random moment.

Believe me, some users have already experienced this: HttpServletResponse seems to periodically send prematurely .

, , ( ). , , - Servlet 3.0 , . : startAsync ?.

+17

-, , , . ,

-2

There is nothing wrong with starting a new thread in Servlet (unlike EJB), so yes, that's fine.

EDITOR: second thought @ Thomasz Nurkevich is right. File download will be stopped.

-5
source

All Articles