@WebServlet(name="asyncServlet",urlPatterns={"/asyncservlet"},asyncSupported=true)
public class AsyncServletTest extends HttpServlet {
private static final long serialVersionUID = 2037346280839395223L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("servlet start:" + new Date());
out.flush();
AsyncContext asyncContext = request.startAsync();
//asyncContext.setTimeout(1000);  //设置线程启动超时时间
asyncContext.addListener(new Mylistener());
MyThread thread = new MyThread(asyncContext);
asyncContext.start(thread);
out.println("servlet end:" + new Date());
out.flush();
}
这是Servlet代码public class Mylistener implements AsyncListener { @Override
public void onComplete(AsyncEvent arg0) throws IOException {
System.out.println("已经完成了");
}
@Override
public void onError(AsyncEvent arg0) throws IOException {
}
@Override
public void onStartAsync(AsyncEvent arg0) throws IOException {
System.out.println("开始了");
}
@Override
public void onTimeout(AsyncEvent arg0) throws IOException {
}}
这是listener