Monday, July 26, 2021

Difference between SendRedirect() and Forward() in JSP Servlet? Example

The difference between SendRedirect and forward is one of the classical interview questions asked during a java web developer interview. This is not just applicable for servlet but also for JSP in which we can use forward action or call sendRedirect() method from scriptlet. Before examining the difference between forward and SendRedirect let’s see what send Redirect method and the forward method does.

SendRedirect ():  

This method is declared in HttpServletResponse Interface.

Signature: void sendRedirect(String url)

difference between sendRedirect and forward in jsp servletThis method is used to redirect the client requests to some other location for further processingthe new location is available on different servers or different contexts. 

Our web container handles this and transfers the request using a browser, and this request is visible in the browser as a new request. Sometimes this is also called a client-side redirect.


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 of by a 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 in two ways either using ServletContext or Request. This is also called a server-side redirect.

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

Or

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



Difference between SendRedirect and Forward

Now let’s see some differences between these two methods of servlet API in tabular format.

Forward()
SendRediret()
When we use the forward method request is transferred to other resources within the same server for further processing.
In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

In case of forward Web container handle all process internally and client or browser is not involved.

When you use the SendRedirect container transfers the request to the client or browser so url given inside the sendRedirect method is visible as a new request to the client.

When forward is called on requestdispather object we pass request and response object so our old request object is present on new resource which is going to process our request

In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
Visually we are not able to see the forwarded address, its is transparent
In the address bar we are able to see the new redirected address it’s not transparent.

Using the forward () method is faster than send a redirect.
SendRedirect is slower because one extra round trip is required because a completely new request is created and the old request object is lost. Two browser requests required.

When we redirect using forward and we want to use the same data in a new resource we can use the request.setAttribute () as we have request object available.
But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.



Example of forwarding and SendRedirect in JSP Servlet:

Any kind of online payment when we use the merchant site will redirect us to the net banking site which is a completely new request it processes our request and again redirect to the merchant site?

In Banking Application when we do login normally we use the forward method. In the case of online banking, we are asked for a username and password if it’s correct some other servlet or resource will handle the request otherwise request has been forwarded to an error page.



Which one is good?

It depends upon the scenario that which method is more useful.

If you want control is transfer to a new server or context and it is treated as a completely new task then we go for Send Redirect.
Normally forward should be used if the operation can be safely repeated upon a browser reload of the web page will not affect the result.

SendRedirect and forward methods are still very useful while programming or working on any web application project using servlet jsp. This is still a popular interview question so don’t forget to revise forward and sendRedirect before appearing for any job interview.


Some more interview post you may find Interesing

6 comments:

  1. does its same as difference between sendRedirect and request dispatcher ?I am writing some jsp in my program and putting code I have writeen on Servlet where I hade used RequestDispatcher , Can you please suggest how Can I redirect or forward in JSP ?

    ReplyDelete
  2. Main difference between redirect and forward is involvement of browser. In HTTP forward, you forward same request to another resource e.g. JSP or Servlet in same server for processing and that resource return response back to client but client doesn't know anything about it. Client which is browser in this case think that it came from same URL which it sent request. While in case of HTTP redirect which is achieved using sendredirect method of Servlet, a response is sent to client or browser which contains address or another resource which can be in same or different server. browser than make another request to get response from there. A good example of HTTP redirect 302 is redirecting from one site to another.

    ReplyDelete
  3. Isn't it there are include and forward action in JSP which can be used to forward a request instead of using RequestDispatcher forward method ?

    ReplyDelete
  4. What is difference in forward and sendredirect in Spring or Struts? does choice of any framework makes it easy or difficult to forward or redirect an HTTP request? I heard there are forward action in Struts framework, does that help in forwarding request?

    ReplyDelete
  5. there is one main difference which he forgot to mention is :
    if we are doing registration os some kind and the data going to store in database. if we use forward() method to store that data and refresh the page on same url, the form will get submitted as amny times as user refreshes the page.
    In sendRedirect() scenario, this wont be the case.

    ReplyDelete
  6. That's interesting, thanks for sharing with us, how did you come to know about that fact? Did you experienced yourself?

    ReplyDelete