问题描述
index.jsp<%@pagecontentType="text/html;charset=UTF-8"language="java"%><html><head><title>Simplejsppage</title><metahttp-equiv=""></head><body>Placeyourcontenthere</body><ahref="/async">AsyncDemo</a></html>=========================================================AsyncDemoServlet.javaimportjavax.servlet.AsyncContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Date;@WebServlet(name="AsyncDemoServlet",urlPatterns={"/async"},asyncSupported=true)publicclassAsyncDemoServletextendsHttpServlet{protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{process(request,response);}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{process(request,response);}privatevoidprocess(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{PrintWriterout=null;try{response.setContentType("text/html;charset=UTF-8");out=response.getWriter();out.println("进入Servlet的时间:"+newDate()+".<br/>");out.flush();//在子线程中执行业务调用,并由其负责输出响应,主线程退出booleanasyncSupported=request.isAsyncSupported();out.println("是否支持异步:"+asyncSupported+".<br/>");out.flush();out.println("下面开始启动异步处理............<br/>");AsyncContextctx=request.startAsync();ctx.start(newProcessTask(ctx));out.println("异步处理已启动,等待其完成............<br/>");out.println("结束Servlet的时间:"+newDate()+".<br/>");out.println("<br/>");out.flush();}catch(IOExceptionex){}finally{//out.close();}}}===================================ProcessTask.javaimportjavax.servlet.AsyncContext;importjava.io.PrintWriter;importjava.util.Date;publicclassProcessTaskimplementsRunnable{privateAsyncContextctx;publicProcessTask(AsyncContextctx){this.ctx=ctx;}publicvoidrun(){PrintWriterout=null;try{out=ctx.getResponse().getWriter();//等待十秒钟,以模拟业务方法的执行Thread.sleep(5000);out.println("业务处理完毕的时间:"+newDate()+".<br/>");out.flush();ctx.complete();}catch(Exceptione){}finally{out.close();}}}=======================================此段是servlet3.0异步代码,在tomcat7.0.21下测试通过。但在Glassfishserver3.1.1下却无法运行,未找出原因,特来请教。WARNING:StandardWrapperValve[AsyncDemoServlet]:PWC1406:Servlet.service()forservletAsyncDemoServletthrewexceptionjava.lang.IllegalStateException:Requestiswithinthescopeofafilterorservletthatdoesnotsupportasynchronousoperationsatorg.apache.catalina.connector.Request.startAsync(Request.java:3865)atorg.apache.catalina.connector.Request.startAsync(Request.java:3817)atorg.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1040)atjavax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:422)atservlet.AsyncDemoServlet.process(AsyncDemoServlet.java:44)atservlet.AsyncDemoServlet.doGet(AsyncDemoServlet.java:27)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:734)atjavax.servlet.http.HttpServlet.service(HttpServlet.java:847)atorg.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)...................................................