Difference between response.sendRedirect() and request.forward()

What is the difference between response.sendRedirect() and request.forward()

Hari

sendRedirect() vs forward()

sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response.

forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.

There are different situations where you want to use one or the other. For example, if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include(). If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect().

Murugesa

The more simple example & tempting/ initimidating  one would be..

that sendRedirect Vs forward..

sendRedirect is the marriage which is leagal as both the parties know each other and faithful

Vs forward.. where one of the party cheats and contacts to third party.. a illicit relationship..

Although views are not mend to be hurting.. but this way... you remember each time and all the time....:--))))

Gaurav

ServletResponse.sendRedirect():
public void sendRedirect(java.lang.String location)
                  throws java.io.IOException

Sends a temporary redirect response to the client using the specified redirect location URL. The URL must be absolute (for example, https://hostname/path/file.html ). Relative URLs are not permitted here. 
Parameters: 
location - the redirect location URL 
Throws: 
java.io.IOException - If an I/O error has occurred.
RequestDispatcher.forward():

public void forward(ServletRequest request,
                    ServletResponse response) throws ServletException,
                    java.io.IOException

Forwards a ServletRequest object from this servlet to a resource (servlet, JSP file, or HTML file) on the server. You can use this method when one servlet does preliminary processing of a request and lets another resource generate the response. 

The ServletRequest object has its path and other parameters adjusted to be relative to the path of the target resource. 

You cannot use forward if the target resource has already returned a ServletOutputStream or PrintWriter object to the servlet. In that situation, forward throws an IllegalStateException.
Parameters: 
request - a ServletRequest object that represents the request the client makes of the servlet 
response - a ServletResponse object that represents the response the servlet returns to the client 
Throws: 
ServletException - if the target resource is a servlet and throws an exception 
java.io.IOException - if an input or output exception occurs 
IllegalStateException - if the target resource returned a ServletOutputStream or PrintWriter object before this method was called

Sendredirect sends a response to the browser asking it to load another page.

Forward transfers control to another servlet or jsp within the server. forward doesn't send anything back to the browser. 

That means that, with forward, the request and response blocks given to the servlet you forward to are the same ones in the servlet you forwarded from, so in particular you can pass attributes in the request object. 

The disadvantage is that the browser doesn't know about the new URL. 

Jaisankar
 
 

What is the difference between sendRedirect & forward methods? - Venu Madhav

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 HtpRequest.
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. 
- The 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.

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 completely with in the web container. When a 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 completely 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. 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.