0

I have written a callback for a web service I am using. That service sends my web app a request and requires me to send a response within 3 seconds.

I call two methods from inside my doPost() method. What I want to do is to return the response after one of those methods has executed and then execute the second method.

Here is a sample of what I am trying to do -

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        try {
            this.doSomething();

            writer.print("200 - OK");
            writer.flush();
            writer.close();

        } catch (Exception e) {

            writer.print("400 - Bad Request");
            writer.flush();
            writer.close();
        }

        this.doSomeThingElse();

    }

Is this allowed? Or is the response sent after the doPost() method executes completely?

Also, under what condition does the thread that the servlet spawns to process the request get blocked? (i.e. it is not returned to the thread-pool)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

It's allowed to invoke doSomethingElse(). But you can't send some data in this method (or later) to the client, since you called writer.close().

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
  • Thanks for looking into this! Could you tell me under what condition does the thread that the servlet spawns to process the request get blocked? (i.e. it is not returned to the thread-pool) – Vighnesh Rao Mar 23 '16 at 12:02
  • sorry, I'm not very familar with servlet threads. Perhaps [this topic](http://stackoverflow.com/q/33568235/1988304) can help you. – JimHawkins Mar 23 '16 at 14:04