Difference Between sendRedirect() and Forward() :
Forward( ) :
javax.Servlet.RequestDispatcher interface.
* RequestDispatcher.forward( ) works on the Server.
* The forward( ) works inside the WebContainer.
* The forward( ) restricts you to redirect only to a resource in the same web-Application.
* After executing the forward( ), the control will return back to the same method from where the forward method was called.
* The forward( ) will redirect in the application server itself, it does'n come back to the client.
* The forward( ) is faster than Sendredirect( ) .
To use the forward( ) of the requestDispatcher interface, the first thing to do is to obtain RequestDispatcher Object. The Servlet technology provides in three ways.
1. By using the getRequestDispatcher( ) of the javax.Servlet.ServletContext interface , passing a String containing the path of the other resources, path is relative to the root of the ServletContext.
RequestDispatcher rd=request.getRequestDispatcher ("secondServlet");
Rd.forward(request, response);
2. getRequestDispatcher( ) of the javax.Servlet.Request interface , the path is relative to current HttpRequest.
RequestDispatcher rd=
getServletContext( ).getRequestDispatcher("servlet/secondServlet"); Rd.forward(request, response);
3. By using the getNameDispatcher( ) of the javax.Servlet.ServletContext interface.
RequestDispatcher rd=
getServletContext( ).getNameDispatcher("secondServlet");
Rd.forward(request, response);
Sendredirect( ) :
javax.Servlet.Http.HttpServletResponce interface
* RequestDispatcher.SendRedirect( ) works on the browser.
* The SendRedirect( ) allows you to redirect trip to the Client.
* The SendRedirect( ) allows you to redirect to any URL.
* After executing the SendRedirect( ) the control will not return back to same method.
* The Client receives the Http response code 302 indicating that temporarly the client is being redirected to the specified location , if the specified location is relative , this method converts it into an absolute URL before redirecting.
SendRedirect( ) will come to the Client and go back,.. ie URL appending will happen.
Response. SendRedirect( "absolute path");
Absolutepath – other than application , relative path - same application.
Conclusion:
forward() runs on the server side where as sendRedirect runs both on the client as well as on the server side thats why the response generated by sendRedirect() is slow as compared to rd.forward().
sendRedirect() you can forward the request to any web application either in the same server or to the another one. Incase of forward() the request has to be forwarded to the same web application.
forward() method holds the previous request and response objects while using sendRedirect(),it will create fresh request and response objects.
sendRedirect() always sends a header back to the client/browser, this header then contains the resource(page/servlet) which you wanted to be redirected. the browser uses this header to make another fresh request. sendRedirect has a overhead since its like any other Http request being generated by ur browser.
response.sendRedirect() sends a response to the browser asking it to load another page, whereas in the case of RequestDipsatcher.forward() control is transferred to another servlet or jsp within the server.
Using sendRedirect() on one server, we call redirect a call to a resource on located on different server which is not possible using forward().
When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container.
sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.
No comments:
Post a Comment