使用AsyncContext异步调用Servlet开发失效

项目背景:

  1. 一个继承HttpServlet类的AsyncDemoServlet类
    一个继承Runnable接口的Executor类
    一个index.jsp,负责转发请求到AsyncDemoServlet
  2. 使用request的startAsync()方法获得AsyncContexc类对象‘actx’
    使用actx的start方法启用异步调用的线程,该方法内参数为新的Executor类实例
  3. 在xml或使用@WebServlet注解部署,其中指定
    true

    asyncSuppored=true

报异常:

13-Sep-2018 04:08:48.623 警告 [http-nio-8080-exec-1] org.apache.catalina.connector.Request.startAsync Unable to start async because the following classes in the processing chain do not support async [org.apache.jasper.servlet.JspServlet]
 java.lang.IllegalStateException: A filter or servlet of the current chain does not support asynchronous operations.
    at org.apache.catalina.connector.Request.startAsync(Request.java:1665)
    at org.apache.catalina.connector.Request.startAsync(Request.java:1658)
    at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
    at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
    at cn.PApudding.AsyncDemoServlet.doGet(AsyncDemoServlet.java:23)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316)
    at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:742)
    at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:712)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:123)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

在百度搜索问题

发现了albert_knag博客上的解答

找了一圈,所有的解决办法都在说可能是参数配置未配置正确,或者是需要在server.xml上配置。最后,在StackOverflow上找到了如下答案

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
  • 我尝试了一下,把这个语句写到Servlet中,结果依然出错

在StackOverFlow中寻找答案

一个老哥也出了这个问题,不过他是写Filter出现的问题。

  • 下面这个是最高赞的解答

Because your servlet and any other filter in chain must have true in web.xml.

所以我在想,我是不是有什么地方没有声明支持async处理

  • 我将目光看向了用来转发请求的index.jsp
  • 我将request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
    使用 小脚本 嵌入到了index.jsp页面中,再运行,便没有出错了。
  <%
        request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
  %>

你可能感兴趣的:(使用AsyncContext异步调用Servlet开发失效)