作者:mobiledu2502926247 | 来源:互联网 | 2014-06-10 08:50
pom.xml:1<dependency>2<groupId>javax.servlet</groupId>3<artifactId>javax.servlet-api</artifactId>4<version>3.0.1</version
pom.xml:
1
2
javax.servlet
3
javax.servlet-api
4
3.0.1
5
provided
6
web.xml:
1
2
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
4
Servlet3-Demo
5
AsyncServlet:
01
@WebServlet(value = "/async-demo", asyncSupported = true)
02
public class AsyncServlet extends HttpServlet {
03
04
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
05
06
public void doGet(HttpServletRequest req, HttpServletResponse res) {
07
AsyncContext aCtx = req.startAsync(req, res);
08
09
executor.execute(new AsyncHandler(aCtx));
10
}
11
12
}
AsyncHandler:
01
public class AsyncHandler implements Runnable {
02
03
private AsyncContext ctx;
04
05
public AsyncHandler(AsyncContext ctx) {
06
this.ctx = ctx;
07
}
08
09
@Override
10
public void run() {
11
System.out.println("Dispatch Time: " + System.currentTimeMillis());
12
13
ctx.dispatch("/index.
jsp");
14
}
15
16
}
摘自 Neron.L的博客