Methods of Request Dispatcher :
1. public void forward(ServletRequest req,ServletResponse resp) throws ServletException,IOException2.
publpublic void include(ServletRequest req,ServletResponse resp) throws ServletException,IOException
Forward mechanism:
If the Servlet – 1 is responsible for preliminary processing and Servlet –2 is responsible to provide complete response to the end user then we should go for forward mechanism. While performing forward , new response object will be created and handed over to the Servlet --2. Hence if any response that is added by the Servlet —1 will not be delivered to the end user. Only the response generated by the Servlet – 2 will be delivered to the end user. Servlet – 2 can also change the response headers which are already set by the Servlet – 1. In the forward mechanism the same request object will be forwarded to the second servlet.. Hence in the form of request scoped attributes information sharing is possible between the components.
When using forward() , the control comes back to the Servlet –1 to finish the execution of remaining statements. After this only the Servlet – 2 will display the response to the end user. In the remaining statements if we are trying to write anything to the response those statements will be ignored by the web container. If any exception is raised then the exception indormation is displayed to the end user instead of Servlet –2 respone. In a servlet we can call forward() only once and mostly as the last statement.