Subject: RequestDispatcher.include vs. RequestDispatcher.forward
Author: WebSpider
Posted on: 09/17/2012 02:39:34 PM
if(failed){
request.setAttribute("request_attr", "request_attr_value");
session.setAttribute("session_attr", "session_attr_value");
request.getRequestDispatcher("service_B.jsp").include(request, response);
...
request.getRequestDispatcher("service_C.jsp").include(request, response);
}else{
...
}
or
if(failed){
request.setAttribute("request_attr", "request_attr_value");
session.setAttribute("session_attr", "session_attr_value");
request.getRequestDispatcher("service_B.jsp").forward(request, response);
return;
}else{
...
}
The key difference between the two is the fact that the
include method leaves the output stream open, whereas the
forward method will close the output stream after it has been invoked.
With
include, you can continue processing any stuff which can be put into output stream or even inject another jsp page (
service_C.jsp); With
forward, however, you had better stop (
return;) unless the remaining task is just background stuff, otherwise,
IllegalStateException would be thrown.
Replies:
References: