public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("before 5");
response.getWriter().write("out form 5 before");
request.getRequestDispatcher("/servlet/Demo6").forward(request,response);
System.out.println("after 5");
response.getWriter().write("out from 5 after");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("before 6");
response.getWriter().write("out from 6 before");
System.out.println("after 6");
response.getWriter().write("out from 6 after");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
控制台输出的顺序是:
before 5
before 6
after 6
after 5
页面上输出的顺序是: (原因: demo5内response 内容被写到缓冲区还没写到浏览器,被转发的时候清空, demo6响应后断开连接,所有demo5 after不会写到浏览器)
out from 6 before
out from 6 after
搜索
复制