request.getRequestDispatcher("")跳转的一些理解

  request.getRequestDispatcher().forward()在应用中跳转时不会中断程序的继续进行,其返回类型为void,其后的程序仍然将执行下去。

例如:

  try {
     su.initialize(config, request, response);
     //限制上传附件的大小为10M.
     su.setTotalMaxFileSize(10 * 1024 * 1024);
     su.upload();
    } catch (ServletException e) {
       e.printStackTrace();
    } catch (SmartUploadException e) {
       e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    } catch (SecurityException e) {
       request.setAttribute("notice", "上传文件过大,上传的文件不能超过10兆!");
       try {
          request.getRequestDispatcher("/portal/viewconfig/notice.jsp")
               .forward(request, response);
       } catch (ServletException e1) {
          e1.printStackTrace();
       } catch (IOException e1) {
          e1.printStackTrace();
       }
    }

在该段执行完成后返回到某个方法,然后又有一个执行

request.getRequestDispatcher("/portal/viewconfig/notice1.jsp").forward(request, response);语句

等最后完成后,它将跳转到第一个forward的地址,其他的地址都没有忽略了。并在后台抛出如下的异常:

java.lang.IllegalStateException: Cannot forward a response that is already committed

意为:在response已经提交后程序不能再一次的跳转!

其中一个较为需要注意的点是:forward跳转的地址以第一个URL为跳转地址。


 

posted @ 2011-08-29 10:40  leanman  阅读(3466)  评论(1)    收藏  举报