100% found this document useful (1 vote)
9K views

Difference Between Request Dispatcher and Send Redirect

RequestDispatcher's forward method forwards the request and response objects to another resource on the server without the client's knowledge. This allows sharing of request information between server-side resources but prevents back/forward navigation in the browser. HttpServletResponse's sendRedirect method instructs the client to make a new request to the specified path, so request information is not maintained and the browser URL and history are updated to allow back/forward browsing.

Uploaded by

S R Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
9K views

Difference Between Request Dispatcher and Send Redirect

RequestDispatcher's forward method forwards the request and response objects to another resource on the server without the client's knowledge. This allows sharing of request information between server-side resources but prevents back/forward navigation in the browser. HttpServletResponse's sendRedirect method instructs the client to make a new request to the specified path, so request information is not maintained and the browser URL and history are updated to allow back/forward browsing.

Uploaded by

S R Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Question

What is the difference between RequestDispatcher's


forward(ServletRequest request, ServletResponse response) method and
HttpServletResponse's sendRedirect(String location) method?

Answer

The forward method of RequestDispatcher will forward the ServletRequest and


ServletResponse that it is passed to the path that was specified in
getRequestDispatcher(String path). The response will not be sent back to the
client and so the client will not know about this change of resource on the server.
This method is useful for communicating between server resources, (servlet to
servlet). Because the request and response are forwarded to another resource
all request parameters are maintained and available for use. Since the client
does not know about this forward on the server, no history of it will be stored on
the client, so using the back and forward buttons will not work. This method is
faster than using sendRedirect as no network round trip to the server and back is
required.

An example using forward:


protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
rd.forward(request, response);
}

The sendRedirect(String path) method of HttpServletResponse will tell the client


that it should send a request to the specified path. So the client will build a new
request and submit it to the server, because a new request is being submitted all
previous parameters stored in the request will be unavailable. The client's history
will be updated so the forward and back buttons will work. This method is useful
for redirecting to pages on other servers and domains.

An example using sendRedirect:


protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.sendRedirect("pathToResource");
}

You might also like