我有以下代码:
@WebServlet(urlPatterns="/Servlet1", asyncSupported=true)
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet1 doGet ..");
AsyncContext ac = request.startAsync();
System.out.println(request.isAsyncStarted());
ac.dispatch("/Servlet2");
System.out.println("Servlet1 doGet after dispatch ..");
}
}
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet2 doGet ..");
}
}
请注意以下输出:
Servlet1 doGet ..
true
Servlet1 doGet after dispatch ..
Servlet2 doGet ..
注意到最后 2 行输出了吗?
我期待(就像转发请求调度程序)
Servlet2 doGet
Servlet1 doGet after dispatch ..
好像 ac.dispatch 创建了一个新线程。
对此有任何想法吗?
谢谢。