Tuesday, 3 June 2014

What is the difference between SendRedirect() and Forward()?

      This is famous question for java interviewer, so you can prepare well for this.

SendRedirect():-

 
HttpServletResponse
interface.

          Syntax for this method is - void sendRedirect(String url)
  e.g
              response.sendRedirect("www.google.com");

       This method is used to redirect client request to some other location for further processing ,the new location is available on different server or different context. Web container handle this and transfer the request using  browser ,and this request is visible in browser as a new request. Some time this is also called as client side redirect.
        
        It can communicate with same or different domains. For example control transfer from http://adnjavainterview.com to http://anydomain.com but forward cannot do this.



Forward(): --

This method is declared in RequestDispatcher interface.

   Signature:  forward(ServletRequest request,ServletResponse response).
        
         This method is used to pass the request to another resource for further processing within the same server, another resource could be any servlet, jsp page any kind of file.This process is taken care by web container when we call forward method request is sent to another resource without the client being informed, which resource will handle the request it has been mention on requestDispatcher object which we can get by two ways either using ServletContext or Request. This is also called server side redirect.
 
  RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
  rd.forward(request, response);
Or

  RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
  rd.forward(request, response);

 ‘session’ is not lost in both forward and redirect.

        To feel the difference between forward and sendRedirect visually see the address bar of your browser,
in forward, you will not see the forwarded address (since the browser is not involved)
in redirect, you can see the redirected address.



When can we use forward and when can we use sendRedirect?

 
Technical scenario: redirect should be used

  1. If you need to transfer control to different domain
  2. To achieve separation of task.
         For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.
         For other than the above two scenarios, forward is efficient to use since it is faster than sendRedirect.




Related Posts:--
1) What are the Implicit Objects in JSP?
2) Custom Tags in Jsp with example
3) Exception Handling Interview questions and answers
4) String Interview Questions and Answers

1 comment:

  1. Hi there, yup this piece of writing is truly good and I have learned lot of things from it concerning blogging.
    thanks.

    ReplyDelete