» » » RequestDispatcher in servlet


  • One servlet delegating request processing duty to other servlet is known as request dispatching.
  • To implement inter-servlet communication and servlet -jsp communication we need request dispatching.
  • When servlet receives a simple client request it need to communicate with any other servlet. if the incoming client request is complex one.
  • A servlet process some portion of the request and remaining portion it need to communicate with other servlet known as inter-servlet communication.

Implementing Request dispatching:

Step 1: Creating RequestDispatcher object.
    
   RequestDispatcher rd=request.getRequestDispatcher("other servlet url name");

Step 2: Dispatching the request to the other servlet.
         
  RequestDispatcher having two methods to switch the control from one servelt to other
  1. public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Includes the content of a resource (servlet, JSP page, or HTML file) in the response.
  2. public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
By calling any of the above two methods request dispatching is implemented

           rd.forward(req, res) or rd.include(req, res)

forward();

  • In case of forward mechanism of  request dispatching one servlet receives the control , performs a portion of processing and switches the control to another servlet for the remaining portion of the processing
  •  That second servlet performs remaining portion of the request processing , produces the response page and handle over to the same web server.

Include():



  • In case of include mechanism of  request dispatching one servlet receives the control , performs a portion of processing and switches the control to another servlet for the remaining portion of the processing. gets control back from the next servlet and produces the dynamic web page on the web server.



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu