public class RequestForward extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
test_send(req,resp);
PrintWriter out= resp.getWriter().append("RequestForward");
}
}
public class index extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("这是POST请求");
// 请求转发后由转发的类做响应,这句话之后的响应无效
String uri ="/index/RequestForward";
req.getRequestDispatcher(uri).forward(req,resp);
PrintWriter out= resp.getWriter().append("index ");
}
public class index extends HttpServlet {
String thisname ="index";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// super.doGet(request, response);
System.out.println(thisname +" GET请求 开始");
request.getRequestDispatcher(uri).forward(request,response);
// response.sendRedirect(index/indexRedirect);
System.out.println(thisname +" GET请求 结束");
}
}
public class indexForward extends HttpServlet {
String thisname ="indexForward";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(thisname +" GET请求 开始");
PrintWriter out= resp.getWriter().append("indexForward 的 PrintWriter ");
System.out.println(thisname +" GET请求 结束");
}
}
转发输出结果:
index GET请求 开始
indexForward GET请求 开始
indexForward GET请求 结束
index GET请求 结束
index GET请求 开始
index GET请求 结束
indexRedirect GET请求 开始
indexRedirect GET请求 结束