首页 新闻 会员 周边 捐助

关于Servlet转发问题

0
悬赏园豆:200 [待解决问题]

为什么Servlet转发后,会回到原来Servlet继续执行输出语句?

为什么转发只有一次请求而Request对象在转发后发生了改变?

代码如下:

@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

          System.out.println("demo2......第一次");
          System.out.println("demo2:"+request);
          request.getRequestDispatcher("/demo1").forward(request, response);
          System.out.println("demo2......第二次");
          System.out.println("demo2:"+request);

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
}

}

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          System.out.println("demo1.......");
          System.out.println("demo1:"+request);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
}

}

输出结果如下:

demo2......第一次
demo2:org.apache.catalina.connector.RequestFacade@4fb8968f
demo1.......
demo1:org.apache.catalina.core.ApplicationHttpRequest@1fdba35b
demo2......第二次
demo2:org.apache.catalina.connector.RequestFacade@4fb8968f

小童子的主页 小童子 | 初学一级 | 园豆:2
提问于:2019-06-11 18:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册