What is the difference between request.forward and requestdispatcher.forward?

Showing Answers 1 - 9 of 9 Answers

debabrat panda

  • Sep 29th, 2006
 

hi

i think ServletRequest and HttpServletRequest have no forward() method

deb

  Was this answer useful?  Yes

123kid

  • Oct 3rd, 2006
 

request dispatcher will be done only on the server side and the client dosent knows about that where as forward will b done on the client side

  Was this answer useful?  Yes

jnr_1808

  • Oct 6th, 2006
 

 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 req, HttpServletResponse res)

throws ServletException, IOException {
  RequestDispatcher rd = req.getRequestDispatcher("pathToResource");
  rd.forward(req, res);
}

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 req, HttpServletResponse res)

throws ServletException, IOException {
  res.sendRedirect("pathToResource");
}

The question should be like - What is the difference between
requestdispatcher.forward and response.sendredirect?


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.
for example


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


for more
In requestdispatcher, the request comes from two objects.
one is request obj.
other is servlet context obj.
in this, URL gets changed, suppose you have logged in in a banking application,
and you want to it forward it to check the balance, then you can use
requestdispatcher.forward (to the resource like jsp, or other).


The sendRedirect(String path) method of HttpServletResponse will tell the
client that it should send a request to the specified path.
for example


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


In send Redirect whenever the client makes any request it goes to the
container, there the container decides whether the concerned servlet can handle
the request or not. If not then the servlet decides that the request can be
handle by other servlet or JSP. Then the servlet calls the sendRedirect() method
of the response object.
In response.sendredirect, you can directly redirect it to the resource (JSP or
other) in this, URL does not get changed.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions