chetanaforum

Difference between forward(request,response) and SendRedirect(url) in Servlet ?


Question of the Day :- 20 March 2006

----------------------------------------------------------------

Explain the differences between forward(request,response) and SendRedirect(url) in Servlet ?

----------------------------------------------------------------

Share this post


Link to post
Share on other sites
Hi,Difference between forward(request,response) and SendRedirect(url) in Servlet ? RequestDispatcher r=request.getRequestdispatcher("good.jsp"); r.forward(request,response); response.sendRedirect("/jj.html");Forward :its a server side request...see its request.getRequestdistatcher()SendRedirect:its a client side request...see its response.sendRedirect()forward:Its url doesnt change on the browserSendRedirect:The user sees the new URLIn real time people doesnt use sendRedirect mostly due to performance Hit.Now a days nobody is using sendRedirect often.i think so..HOW THE PROCESS WILL HAPPEN IS:1.forward: 1 first the url is typed in the browser. 2 request then goes to server and then container and checks with DD for the url matching 3 the servlet then tells that it should go to another web appln. 4 Now in this moment it will call RequestDispatcher and dispatch the request to other page ..here good.jsp 5 Now the browser as usual get the responsse from server thro container and displays the page.The browser doesnot know the resultant page i mean good.jsp only created the result in the current page2.sendRedirect: 1 first the url is typed in the browser. 2 request then goes to server and then container and checks with DD for the url matching 3 the servlet then tells that it should go to complete different URL 4 Now Servlet calls sendRedirest on response and then it goes to browser thro server including the status code 301 and location Header as the value 5 Now thw browser gets the status code and location header.so that it makes a new request using the URL i mean the Location header value(Location header value=URL) in the prevoious response. 6 THE USER DOES NOTICE THE URL CHANGE IN BROWSER BAR 7 then as usual the request go and hit the container thro server and looks for DD the matching servlet and then return the response and the o/p will get displayed...regards,kavitha[/font]Hi,Difference between forward(request,response) and SendRedirect(url) in Servlet ? RequestDispatcher r=request.getRequestdispatcher("good.jsp"); r.forward(request,response); response.sendRedirect("/jj.html");Forward :its a server side request...see its request.getRequestdistatcher()SendRedirect:its a client side request...see its response.sendRedirect()forward:Its url doesnt change on the browserSendRedirect:The user sees the new URLIn real time people doesnt use sendRedirect mostly due to performance Hit.Now a days nobody is using sendRedirect often.i think so..HOW THE PROCESS WILL HAPPEN IS:1.forward: 1 first the url is typed in the browser. 2 request then goes to server and then container and checks with DD for the url matching 3 the servlet then tells that it should go to another web appln. 4 Now in this moment it will call RequestDispatcher and dispatch the request to other page ..here good.jsp 5 Now the browser as usual get the responsse from server thro container and displays the page.The browser doesnot know the resultant page i mean good.jsp only created the result in the current page2.sendRedirect: 1 first the url is typed in the browser. 2 request then goes to server and then container and checks with DD for the url matching 3 the servlet then tells that it should go to complete different URL 4 Now Servlet calls sendRedirest on response and then it goes to browser thro server including the status code 301 and location Header as the value 5 Now thw browser gets the status code and location header.so that it makes a new request using the URL i mean the Location header value(Location header value=URL) in the prevoious response. 6 THE USER DOES NOTICE THE URL CHANGE IN BROWSER BAR 7 then as usual the request go and hit the container thro server and looks for DD the matching servlet and then return the response and the o/p will get displayed...regards,kavitha[/font]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now