Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher vs sendRedirect

 
louise rochford
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dave
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
u can use the RequestDispatcher.include method for calling the second servlet and then serve the resulting page to the browser.
hope this helps
 
Priya Rangan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic