日期:2014-05-20  浏览次数:21022 次

【100分】请教关于Servlet3.0 + Tomcat7.0.x 异步调用的问题
Java code

@WebServlet(urlPatterns="/test",asyncSupported=true)
public class ModuleAsyncServlet extends HttpServlet {
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            Writer out = response.getWriter();
//            request.startAsync(); //报异常
            AsyncContext ctx = request.getAsyncContext();//null
            System.out.println(request.isAsyncSupported());  //false




异常信息如下

java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1594)
at org.apache.catalina.connector.Request.startAsync(Request.java:1587)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1024)
at org.async.servlet.ModuleAsyncServlet.doGet(ModuleAsyncServlet.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



=======================


当我提交一个 test请求的时候 , 可以进入到 get 方法中
并执行,当执行到request.startAsync()的时候, 会报如上异常

环境 jdk.1.6.0_22 apache7.0.6 正式版 


=======================================

这个问题解决一上午了, 郁闷 不知道咋办



完整代码如下

Java code

package org.async.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/test",asyncSupported=true)
public class ModuleAsyncServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            Writer out = response.getWriter();
            request.startAsync();
            AsyncContext ctx = request.getAsyncContext();
            System.out.println(request.isAsyncSupported());
            //AsyncContext ctx = request.startAsync();
            new Thread(new Executor(ctx)).start();
            out.write("结束Servlet的时间:" + new Date() + ".");
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPost(req, resp);
    }

}

class Executor implements Runnable {
    private AsyncContext ctx = null;

    public Executor(AsyncContext ctx) {
        this.ctx = ctx;
    }

    public void run() {
        try {
            // 等待十秒钟,以模拟业务方法的执行
            Thread.sleep(10000);
            PrintWriter out = ctx.getResponse().getWriter();
            out.println("业务处理完毕的时间:" + new Date() + ".");
            out.flush();
            ctx.complete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}







------解决方案--------------------
java.lang.IllegalStateException: Not supported. 不被支持

给你例子看看 
tomcat7和Servlet3之异步特性体验
http://sunqi.javaeye.com/blog/708823
http://www.360doc.com/content/10/0920/08/1720440_55016710.shtml