Tomcat7實現Servlet3異步請求

jopen 12年前發布 | 2K 次閱讀

pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>

    <scope>provided</scope>
</dependency>

web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>Servlet3-Demo</display-name>
</web-app>

AsyncServlet:

@WebServlet(value = "/async-demo", asyncSupported = true)
public class AsyncServlet extends HttpServlet {

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);

    public void doGet(HttpServletRequest req, HttpServletResponse res) {
        AsyncContext aCtx = req.startAsync(req, res);

        executor.execute(new AsyncHandler(aCtx));
    }

}

AsyncHandler:

public class AsyncHandler implements Runnable {

    private AsyncContext ctx;

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

    @Override
    public void run() {
        System.out.println("Dispatch Time: " + System.currentTimeMillis());

        ctx.dispatch("/index.jsp");
    }

}

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!