Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect vs RequestDispatcher

 
Ankit Mishra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know one thing why is it so that in some cases response.sendRedirect works and in some cases RequestDispatcher works.

I know the differences between the two but the thing i don't understand is that i want to send the code to jsp file but i am not able to get the results in there. while i do it with the help of requestdispatcher i am able to see the results on my servlet.

See the example below:

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the RequestDispatcher, everything happens on the server side, the client will not notice any form of redirect. This way you can also take advantage of the 4 scopes the servlet spec offers.

Using a redirect instead works on the client side. The container sends a (I thnk 301) response code indicating that the resource is to be found at another location, and the browser will follow. Passing information is only possible using the querystring.

So, for the general workflow of your application, use a dispatcher, since the client should not know or bother about your internal organization at all.
 
Ankit Mishra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sebastian for your response.

In the above code I have not commented the response.sendRedirect which gives me nothing on my jsp page. How can i then make a thing which will show result on my jsp? RequestDispatcher is working perfectly here it is showing the desired result.

Thanks for your advice too will keep that in mind.
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try it this way,
response.sendRedirect("/music.jsp"); // try it with /.

/ makes the built URL relative to the web container.

if it still not works , check your path.(whether your .jsp is inside another folder)


 
Ankit Mishra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Priya

Thanks for your response. My jsp file is under my application directory only only not in any other folder. The thing you asked me to do is giving 2 things on IE 8 its not showing file.
In Firefox its giving a blank jsp .
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What happens if you access music.jsp from browser directly through URL. JSPs inside WEB-INF cannot be accessed through browser directly.
 
Ankit Garg
Sheriff
Posts: 9578
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to understand attribute scopes clearly. If you set an attribute into request scope, then it is only available for that request. If you use send redirect, then the client's browser sends another request to the server and thus the attribute in request scope is lost. To make it work, you'll have to set the attribute in session scope. Since you are using / in the beginning of the redirect method, you'll have to add the servlet context path to it to make it work (unless your application is deployed on ROOT context path)...
 
Ankit Mishra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your help.

@ Ankit ya true i have to work on these things a bit more. Thanks for clearing my concept.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic