Request Dispatching In Servlets Servlets / Request Dispatcher RequestDispatcher Vs SendRedirect What Is A Request Dispatcher And How Does It Work What Is A Request Dispatcher And How Do I Use It Include Forward Redirect

Servlet Request Dispatching

Delegating the request from one servlet to another or transfer of excution from one servlet to another is known as request dispatching in servlet.

In another ways we can define it as interservlet communication,which means one servlet calling another servlet.This concept can be used in cases where some piece of code is needed to be written in different servlets.Thus, in such cases we can write that code in a single servlet and keep on calling that code from different servlets.

There are three ways in which request delegation can be done. These ways are as follows :






1. Include : This method suggests that the response from the another servlet be included in the response of the first  servlet. This is equivalent to copy pasting the response of one servlet into another.In case of inclusion,the response which the client receives is the append of both including and the included servlets.However,for the client it appears that the including servlet only has responded.Following line of code is used to include a servlet response into another servlet :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");


Here RequestDispatcher object is needed .We can see that request and response objects of the first servlet are passed to the servlet which we wish to include through include method so that request remains the same and the included servlet can write to the response of the including servlet and it appears that including servlet itself has processed the request.

2. Forward : It works same as  include except for the difference that the reponse from the first servlet is flushed out and the reponse from the second servlet only is visible.Example code is :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");


Here also it appears to the client that the first servlet only has processed the request.

3. Redirect : In the case of redirect the first servlet issues a new request to a another servlet to which redirection has happened.In this case client sees a new request in the browser url bar and can realize that the response comes from the second servlet.Example code is :

response.sendRedirect("url-pattern of the servlet");


Here we can see that response object is used.This done for the reason that the issuing of the new request at the client side,in the browser url bar has to be done by the first servlet.


Number of View :4720

Tags: , , , , , , , ,

  1. No comments yet.

Content Protected Using Blog Protector By: PcDrome.