Request Dispatcher - forward() and include() method

  • RequestDispatcher is an interface in javax.servlet package. The subclass is implemented by container vendor.
  • It has two methods: forward() and include().
 1.) forward() method 
  • forward() method is used to forward the request from:
         --Servlet to HTML
         --Servlet to JSP
         --Servlet to Servlet
         --JSP to HTML
         --JSP to JSP and 
         -- JSP to Servlet
  • We can use many forward() methods in one servlet conditionally to forward the control to other resource but only one forward will be executed.
       RequestDistpatcher rd= null;
       rd = request.getReqeustDispatcher("hello.jsp");
       rd.forward(request, response);
  • When we have other statement after the forward() method then those statement will be executed but we can't write any content in the response stream.
2.) include() method: 
  • include() method is used to include the response of servlet or jsp in another servlet or jsp.
  • We can place many include() methods in one servlet to include many resources and all include methods will be executed. 

1 comment :