Finishing a HttpServletResponse but continue processing

comet, http, java, servlets

Solution

Here's how I've handled this situation:

- When the app starts up, create an `ExecutorService` with `Executors.newFixedThreadPool(numThreads)` (there are other types of executors, but I suggest starting with this one)

- In `doPost()`, create an instance of `Runnable` which will perform the desired processing - your task - and submit it to the `ExecutorService` like so: `executor.execute(task)`

- Finally, you should return the HTTP Status `202 Accepted`, and, if possible, a `Location` header indicating where a client will be able to check up on the status of the processing.

I highly recommend you read Java Concurrency in Practice, it's a fantastic and very practical book.

Problem

I have a situation that seems to fit the Async Servlet 3.0 / Comet situation but all I need to do is return a 200 response code (or other) after accepting the incoming parameters. Is there a way for a HttpServlet to complete the http request/response handshake and yet continue processing? Something like... ``` doPost( req, response ) { // verify input params... response.setStatus( SC_OK ); response.close(); // execute long query } ``` EDIT: Looking at the javax.servlet package - the proper phrasing to my question is How do I commit a response? as in Servlet.isCommitted()

Original source