What is difference between
1)sendredirect(response.sendRedirect("test.jsp")
2)requestdispatch(request.getRequestDispatcher("result.jsp").forward(request,response);
I want to explain this in very simple terms................with an example
of enquiry center.
RequestDispatching
------------------------
1)Anil calls person A in enquiry center and introduces himself as "Anilkumar".
2)He enquires about TrainTimings to Goa.
3)A who receives the call doesn't know the timings.. so what A does is.., he
calls another person B in the enquiry center who knows the timings
of GOA and says.. to B that.. a person called "Anil" want to know the
train timings... of GOA.
A and B are persons at same place (Enquiry Center).
4)In this scenario.., B now knows that person who called is "Anil" because..
A told the person's name to B before A transfers call to B.
5)Now B gives train timings to Goa(response) and ends the call..
So,compare now above example with browser(Anil) and Servlet(A) and jsp(B).
1)Anil requests HelloServlet(A)
2)Servlet(A) performs some logic and when it decides to transfer control to jsp(B)
it calls forward method of requestdispatcher to transfer control to jsp(B).
3)In the above example... A passed the name of the person(Anil) to B before
transferring the call to B.Likewise in request dispatching... what ever the
request you sent to HelloServlet will be transferred to.. jsp.(request and response
objects which are passed to HelloServlet will be transferred to jsp page(B).
RequestDispatcher disp = req.getRequestDispatcher("forwardPage.jsp");
disp.forward(req, resp);--You can see the request and response
objects are being passed to servlet/jsp from LoginServlet.
SendRedirect
----------------
1)Anil calls person A in enquiry center and introduces himself as "Anilkumar".
2)He enquires about TrainTimings to Goa.
3)A who receives the call doesn't know the timings.. so what A does is, he gives
phone no of another person in enquiry center and asks Anil to call that person.
4)So,Anil calls the new person and introduces himself as "Anilkumar".
5)He requests goa train timings and new person gives response(timings).
1)Browser requests(First request) HelloWorldServlet
2)HelloWorldServlet sends response with status code "301" and a "Location
header with a URL as the value.(New phone no)
(Eg: Location: handler.jsp-- If you compare with above example this is the
new phone no given by person A).
3)When browser sees "301" response it understands that it has to make
new request to URL given in the Location header.
4) So,browser now makes new request(second request) to the url(handler.jsp) given by HelloWorldServlet.
5)Browser receives the response from handler.jsp
So, finally in request dispatching... Browser- requests
->Servlet->Servlet transfers control to JSP along with request and
response objects->Jsp processes request and sends response to
-Browser
SendRedirect
Browser requests Servlet->Servlet sends 301 status code with new url in Location
header to browser->browser issues new request to new url ->new url processes
the request and response is sent back to browser.
If you observe.. In send redirect there are 2 request calls from Browser........
Where as in request dispatching only one request is there.. request transfers
to another jsp at server side.
Hope you learnt......... RequestDispatching and Sendredirect mechanisam...
No comments:
Post a Comment