我只想了解在执行转发的servlet之后返回到调用servlet的目的。
下面的示例仅显示在执行转发的servlet之后,控制权返回到调用servlet。
//servlet1 Code (Forwarding to servlet2)
@WebServlet("/servlet1")
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.forward(request, response);
System.out.println("Returned to Calling Servlet");
}
}
//servlet2 Code (returning control to servlet1)
@WebServlet("/servlet2")
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = "to Servlet2";
out.print("Welcome " + n);
}
}
欢迎使用Servlet2(在浏览器上)
返回到调用Servlet(在控制台上)
提交响应后,为何返回到 servlet1