What's the purpose of async-supported in web.xml?

jiafu picture jiafu · Mar 29, 2012 · Viewed 37.7k times · Source
<servlet>
        <description>xxx</description>
        <servlet-name>xxx</servlet-name>
        <servlet-class>com.xxx.yyy</servlet-class>
        <async-supported>true</async-supported>
</servlet>

What's the purpose of async-supported in the servlet's web.xml configuration file? What case I can use it in?

Answer

egerardus picture egerardus · Mar 16, 2013

Ironically, I was looking for the syntax of how to write this property in tomcat's web.xml and this is the first search item I opened from google - it's written correctly too (it works), so thanks.

To answer your question though, this allows the servlet to store incoming requests for later response. It frees up the thread used to handle the request so it can be used elsewhere until the server is ready to send the response.

For practical purposes, with this configuration you can set-up a servlet that will (in effect) push data to the client (after the client sends the initial request to the server).

This technique replaces the need for unnecessary timed requests from a client to get data that can change at uncertain intervals. And it does it in a scalable manner by not hanging onto the thread.


Some example use-cases include:

Chat applications, when one client types a message you want it to appear instantly to the other client.

Email apps, to allow clients to view e-mails as soon as they are received by the e-mail server.

I've also used it to send input change updates to a browser from a Programming Logic Controller for automation tasks.

Here's a good tutorial on it. This also covers some nut and bolts in java.