GeekInterview.com
Series: Subject: Topic:
Question: 62 of 177

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

Asked by: Interview Candidate | Asked on: Sep 22nd, 2006
Showing Answers 1 - 5 of 5 Answers
debabrat panda

Answered On : Sep 29th, 2006

hii think ServletRequest and HttpServletRequest have no forward() methoddeb

  
Login to rate this answer.
123kid

Answered On : 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

  
Login to rate this answer.
jnr_1808

Answered On : 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");}

Yes  1 User has rated as useful.
  
Login to rate this answer.
sunil.yadav

Answered On : Jan 31st, 2007

View all answers by sunil.yadav

The question should be -- What is the difference between response.sendRedirect(url) and RequestDispatcher.forward(url)?

  
Login to rate this answer.
muktasharad

Answered On : Sep 3rd, 2008

View all answers by muktasharad

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.