Pages

Wednesday, November 10, 2010

sendRedirect() vs. 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 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. 
Code:
sendRedirect()
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
public class MyServlet extends HttpServlet {    
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {         
       //redirect the user depending on the value of the 'go' param
        String destination = getInitParameter("go");         String contextPath = request.getContextPath();         
         if(destination == null || destination.equals(""))             throw new ServletException(              "Missing or invalid 'go' parameter in " +                getClass().getName());         
        if(destination.equals("weather"))         //ensure URL rewriting
            response.sendRedirect(response.encodeRedirectURL(contextPath + "/weather") );         
         if(destination.equals("maps"))         //ensure URL rewriting
            response.sendRedirect(response.encodeRedirectURL(contextPath + "/maps") );     }
}
forward():
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class Main extends HttpServlet {
  protected void service(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/p.jsp");
    dispatcher.forward(request, response);   }

}
(from http://www.sap-img.com/java/difference-between-response-sendredirect-and-request-forward.htm)

7 comments:

  1. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.

    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  2. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  3. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    AWS training in chennai

    AWS Training in Bangalore

    ReplyDelete