异步Servlet异常

时间:2012-06-10 17:40:14

标签: java java-ee-6 tomcat7 servlet-3.0

我是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:不支持。我正在使用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) 

2 个答案:

答案 0 :(得分:8)

如果您使用tomcat作为您的Web服务器,那么您应该主张您将使用ascat请求到tomcat

在启动异步请求之前输入此行代码

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

我希望你能得到你想要的东西

答案 1 :(得分:4)

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