It's not a secret anymore!
The moose likes Servlets and the fly likes RequestDispatcher vs sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher vs sendRedirect" Watch "RequestDispatcher vs sendRedirect" New topic

RequestDispatcher vs sendRedirect

louise rochford
Ranch Hand

Joined: Apr 04, 2002
Posts: 119
Does anyone have an opinion on whether it is better to use a RequestDispatcher over sendRedirect() or vice versa?
I want to link 2 servlets together.
Currently the first servlet returns a (useless)page to the user. The user then has to click a link to trigger the processing of the second servlet which returns a second (useful) page.
I want to get rid of the first page & seemlessly link straight into the second without user intervention.
I'm inclined to use sendRedirect(), but I suspect this may inconvenience the user - I'm not sure how this fits with WAI accesssibility guidelines (7.5). Also I don't see the point of returning info to the client just for it to request info from the server - isn't it better just to call the second servlet from the server itself?
Is one method more portable than the other???
Christopher Farnham

Joined: Sep 18, 2002
Posts: 12
I'm writing this, hoping that other people will chime in and verify what I'm writing.
I believe that sendRedirect(url) tells the user's browser to make a new request for the specified URL, whereas RequestDispatcher returns the specifed page as if it were the result of the first request.
Because of this, I usually use RequestDispatcher because it is one less round-trip between the browser and server. Also, you have more flexibility because you can add attributes directly to the request before passing the request object in to the RequestDispatcher's forward method.
Going back to your first problem. Why aren't you combining the logic of the two servlets so that they are one servlet? And/Or have you looke into filters?

Christopher Farnham<br />Boston, MA<p>"Perfect is the Enemy of Good"
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

About the only advantage to the sendRedirect method and the main reason I use it is that it can 'disconnect' the process flow.
If you have form.jsp that submits to process.jsp, after the form is submitted and processed in process.jsp, the URL will still say process.jsp, even if process.jsp forwards to another page to manage the display.
If the user reloads the page, the data will be sent to process.jsp again and may result in duplicate data in the database or worse issues
In this situation I tend to get process.jsp to process the data and then redirect to view.jsp. Reloading will just reload view.jsp rather than resubmitting the form.
Kiran Kumar
Ranch Hand

Joined: May 18, 2001
Posts: 107
u can use the RequestDispatcher.include method for calling the second servlet and then serve the resulting page to the browser.
hope this helps

Thanks<br /> <br />Kiran <br /> <br />SCEA, SCJP 1.4,<br /> <br />"First they ignore u, then they laugh at u, then they fight u, <br />then u will win<br /> <br />Mahatma Gandhi"
Priya Rangan

Joined: Sep 17, 2002
Posts: 10
i quote this from 'core Servlets and JSPs' by Marty Hall. I hope it makes things clear to you.
Although a servlet can forward the request to arbitrary locations on the same
server, the process is quite different from that of using the sendRedirect
method of HttpServletResponse (see Section 6.1). First, sendRedirect
requires the client to reconnect to the new resource, whereas the forward
method of RequestDispatcher is handled completely on the server. Sec-ond,
sendRedirect does not automatically preserve all of the request data;
forward does. Third, sendRedirect results in a different final URL,
whereas with forward, the URL of the original servlet is maintained.
I agree. Here's the link:
subject: RequestDispatcher vs sendRedirect
It's not a secret anymore!