Asynchronous servlet

Let's split our AsyncServlet class into pieces so we can understand it:

@WebServlet(urlPatterns = "/AsyncServlet", asyncSupported = true)

Here, we defined our servlet for accepting async behavior by using the asyncSupported param:

AsyncContext asyncCtx = request.startAsync();

We used the request being processed to start a new async context.

Then we start our async process:

asyncCtx.start(() -> {...

And here we print our output to see the response and finish the async process:

                asyncCtx.getResponse().getWriter().write("Async                 process time: "                 + timeElapsed + " milliseconds");                asyncCtx.complete();

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.