4

我是 JavaEE 6 的新手,我了解到它支持称为 AsyncServlets 的异步 servlet。所以我尝试使用JavaEE6创建一个小程序。

这是我的 servlet 代码

@WebServlet(name = "AsyncServlet", urlPatterns = {"/AsyncServlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        AsyncContext aCtx = request.startAsync(request, response);
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(new MyClass(aCtx));
        System.out.println("Original thread is freed");
    }




} 

但是当我尝试运行 servlet 时,我得到一个异常 java.lang.IllegalStateException: Not supported。我使用 tomcat 7.0.14 作为服务器。我没有创建 web.xml。这段代码的问题在哪里?

编辑:

这是异常的完整堆栈跟踪

SEVERE: Servlet.service() for servlet [AsyncServlet] in context with path [/AsyncTest] threw exception
java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1618)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
at com.icbt.demo.servlet.AsyncServlet.doGet(AsyncServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662) 
4

2 回答 2

9

如果你使用 tomcat 作为你的 web 服务器,那么你应该保持你将使用 asyc 请求到 tomcat

在开始异步请求之前把这行代码

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

我希望你能从中得到你想要的

于 2014-05-31T09:08:29.830 回答
4

如果应用程序尝试启动异步操作并且请求处理链中存在不支持异步处理的 servlet 或 servlet 过滤器,则会抛出 IllegalStateException。所以可能有一些错误。请检查您的代码。

于 2012-09-05T06:52:23.420 回答