IllegalStateException:不支持 AsyncContext.startAsync(req, res) [英] IllegalStateException: Not supported on AsyncContext.startAsync(req, res)
问题描述
我创建了一个 servlet 3.0 来探索异步请求处理:
I have created a servlet 3.0 to explore asynchronous request processing:
@WebServlet(name="MyTest", urlPatterns={"/MyTest"}, asyncSupported=true)
public class MyTest extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
AsyncContext tmp = req.startAsync(req, res);
...
}
}
但是当 .startAsync(...)
被调用时,我得到一个 IllegalStateException
.我知道 Javadoc 提到了该异常,但我确实明确启用了异步(参见 WebServlet
注释).我使用的是随 NetBeans 一起提供的 Tomcat 7.0.11.0.
but I get an IllegalStateException
when .startAsync(...)
is called. I know the Javadoc mentions that exception, but I did explicitly enable async (c.f. WebServlet
annotation). I am using Tomcat 7.0.11.0 delivered with NetBeans.
我可以确认 req.isAsyncSupported()
返回 false.我究竟做错了什么?我还需要做什么才能启用异步处理?
I could confirm that req.isAsyncSupported()
is returning false. What am I doing wrong? What more do I need to do to enable async processing?
我尝试实现以下 example 并遇到了同样的问题.
I tried to implement the following example and got the same issue.
推荐答案
我查看了 Tomcat 的代码,发现 asyncSupported 变量必须显式设置为 true.这就是为什么你得到 req.isAsyncSupported() == false
.
I checked out Tomcat's code and saw that the asyncSupported variable has to be explicitly set to true. That's why you are getting req.isAsyncSupported() == false
.
您可以尝试通过以下方法之一将 HttpServletRequest 对象中的 async 属性设置为 true.
You could try to set the async attribute in the HttpServletRequest object to true by one of the following methods.
req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
或
((org.apache.catalina.connector.Request)req).setAsyncSupported(true);
希望有帮助.
这篇关于IllegalStateException:不支持 AsyncContext.startAsync(req, res)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!