Servlet 3.0 Async-supported does not work

Ben picture Ben · Nov 5, 2010 · Viewed 42.4k times · Source

Here is my web.xml

    <filter>
    <filter-name>pollingTest</filter-name>
    <filter-class>
        webapp.controller.core.servlet.PollingService
            </filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>pollingTest</filter-name>
    <url-pattern>/app/poll</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ASYNC</dispatcher>
</filter-mapping>

Here is the class:

public class PollingService implements Filter {
Logger logger = LoggerFactory.getLogger(getClass());

@Override
public void destroy() {
    logger.info("Destroy");
}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {

    logger.info("Running");
    req.startAsync(req, res);
    this.doFilter(req, res, chain);
    return;
}

@Override
public void init(FilterConfig arg0) throws ServletException {

    logger.info("Init=");
}

}

I run it on glassfish and also on tomcat 7, got exception:

java.lang.IllegalStateException: Request is within the scope of a filter or servlet that does not support asynchronous operations
at org.apache.catalina.connector.Request.startAsync(Request.java:3657)
at org.apache.catalina.connector.Request.startAsync(Request.java:3633)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1053)
at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:450)

Who can help me on this? thanks a lot.

Answer

Peter Karabinovich picture Peter Karabinovich · Apr 28, 2011

Because your servlet and any other filter in chain must have <async-supported>true</async-supported> in web.xml.