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():
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.
public void forward(ServletRequest request,
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.
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.
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.
2. getRequestDispatcher( ) of the javax.Servlet.Request
interface , the path is relative to current HtpRequest.
3. By using the getNameDispatcher( ) of the javax.Servlet.ServletContext
interface.
Sendredirect( ) : javax.Servlet.Http.HttpServletResponce
interface
Response. SendRedirect( "absolute path");
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?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|