From:htt p:// javarevisited.blogspot.ca /2011/09/sendredirect-forward-jsp-servlet.html
Difference
 between SendRedirect and forward is one of classical interview 
questions asked during 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 
difference on forward and SendRedirect let’s see what send Redirect 
method and forward method does.
SendRedirect ():  
This method is declared in HttpServletResponse Interface.
Signature: void sendRedirect(String url)
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.our 
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.
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);
E.g.
     String action = request.getParameter("action");
        if (action != null) {
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            if ("include".equalsIgnoreCase(action)) {
                rd.include(request, response);
            } else if ("forward".equalsIgnoreCase(action)) {
                rd.forward(request, response);
            }
      } 
Difference between SendRedirect and Forward
Now let’s see some difference between these two method of servlet API in tabular format.
   
Forward() 
 |    
SendRediret() 
 |   
   
When we use forward method request is transfer to other resource   within the same server for further processing. 
 |    
In case of sendRedirect request is transfer to another resource   to different domain or different server for futher processing. 
 
 |   
   
In case of forward Web container handle all process internally and   client or browser is not involved. 
 
 |    
When you use SendRedirect container transfers the request to   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 address bar we are able to see the new redirected address it’s   not transparent. 
 
 |   
   
Using forward () method is faster then send redirect. 
 |    
SendRedirect
 is slower because one extra round trip is required   beasue completely 
new request is created and old request object is lost.Two   browser 
request requird. 
 
 |   
   
When 
we redirect using forward and we want to use same data in   new resource
 we can use 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. 
 |   
 
 
评论
发表评论