Forwarding and redirection differences and relations

As a programmer Java Web development, when using the Servlet / JSP, we need to know the differences and achieve contact page jump of two ways: namely forwarding and redirection difference.

What is forward

First, the client sends a request to the server, the server match is found Servlet, and assign it to execute. When the Servlet executed, it calls getRequestDispacther () method, forwards the request to the specified JSP, the whole process is done on the server side, and in which the same request is completed, and thus are shared Servlet and JSP with a Request, which put all the stuff in the Servlet, the JSP can be taken out, therefore, JSP can result getAttribute () out, getAttribute () after it completed the implementation of the results returned to the client. The whole process is a request, a response.

request.getRequestDispatcher("/yanggb.jsp").forword(request, response);

What is redirection

First, the client sends a request to the server, the server match is found Servlet, and assign it to execute. When the Servlet executed, it will call sendRedirect () method, immediately return the response to the client end, you have to respond again to tell the client sends a request to access JSP. Immediately after the client receives the request, it will immediately issue a new request to ask for JSP, here, two requests without disturbing each other, independent of each other. This means that anything in front of Request which setAttribute (), and in the back of the request which are not available. Thus, in the sendRedirect () there are two requests and two responses (the server 302 transmits a status code to the browser, and after receiving a location request message requests header, browser again according to the redirection address).

response.sendRedirect("/yanggb.jsp");

Differences and relations

1.RequestDispatcher.forward () method can only forward the request to the component with a WEB application; and HttpServletResponse.sendRedirect () method can be redirected to other resources in the current application, but also can be redirected to the same site other applications on the resources, or even use an absolute URL to redirect resources to other sites. If you pass HttpServletResponse.sendRedirect () method in relative URL [/] at the beginning, it is relative to the root directory of the entire WEB site; specify when you create RequestDispatcher object to the relative URL [/] at the beginning, it is relative to the current WEB the root directory of the application.

2. Call HttpServletResponse.sendRedirect () method to redirect after the end of the process of access, URL displayed in the browser address bar will change into the initial target URL redirection URL address; and calls RequestDispatcher.forward () method after forwarding the request, the browser address bar to keep the original URL address unchanged.

The results 3.HttpServletResponse.sendRedirect () method directly respond to the request of the browser, the response is to tell the browser to re-access request to another URL, this process has a nickname like the browser to find people write Zhang three borrow money, Joe Smith replied that there is no money, so the browser go by John Doe and John Doe current mailing address to tell the browser. So, the browser and mailing address provided by John Doe John Doe wrote to borrow money, send money to John Doe received a letter after the browser. Visible browser issued a total of two letters and received two responses, the browser know that he borrowed money from the hands of John Doe. The RequestDispatcher.forward () method within the server forwards the request to another resource, the browser knows only issued in response to a request and get a result, does not know the internal behavior occurs forwarding server program. This process is like man nicknamed browser write to borrow money to find Joe Smith, Joe Smith had no money, so Joe Smith to find John Doe borrowed some money, you can even add some of their own money, then the money remitted to the browser. Visible, the browser only sent a letter and received a reply, he only knew where to borrow money from Joe Smith does not know some of the money out of the hands of John Doe.

4.RequestDispatcher.forward () share the same Request Response object between the object and the caller and the callee method, which belong to the same access request and response process; and an HttpServletResponse.sendRedirect () caller and callee use response request object and the respective objects in two separate access request and response process. For the jump between the internal resources of the same WEB applications, especially prior to the request to skip some preliminary pretreatment, and to use HttpServletRequest.setAttribute () method of transmission pre-processing results, it should be used RequestDispatcher.forward ()method. WEB redirection between different applications, in particular, to be redirected to another resource on the WEB site of circumstances, should use HttpServletResponse.sendRedirect () method.

5. Whether RequestDispatcher.forward () method, or HttpServletResponse.sendRedirect () method, before calling them, can not have been the actual output content to the client. If the buffer already have some content that will be removed from the buffer.

Two kinds of ways to get a jump object

// 获得转发对象getRequestDispatcher()
HttpServletRequest(httpServletRequest).getRequestDispatcher
ServletContext.getRequestDispatcher();
// get the redirect target sendRedirect () 
HttpServletResponse (HttpServletResponse) .sendRedirect ();

A common interpretation

In short, the forwarding server behavior, redirect the client behavior. Why do you say, which depends on the workflow of two actions:

Forwarding process: the client browser sends an http request -> web server accepts this request -> call a method inside the completion of the request process and forward movement inside the container -> Send a target resources to the customer; here, the forwarding path must be the same url container under a web, it can not be turned up to other web path, the intermediate transmission request is within its own container. In the client browser bar displays the path is still the path that first visit, that customers do not feel a forwarding server. Forwarding behavior is only a browser to access requests.

Redirection procedure: http client browser sends a request -> status code 302 sent in response to a new location and a corresponding client browser to the web server accepts -> client browser 302 in response to a discovery, and then automatically sends a new http request, the new location is requested url address -> server based on the request sent to the customer to find resources. Here location can be redirected to any URL, the browser since it is re-issued the request, the request would have no concept of the transfer. In the client browser displays the path bar to redirect its path, customers can observe the change of address. Redirect the browser to do a behavior is at least twice the access request.

Common Interpretation II

Redirect, in fact, twice Request. First, the client request to A, then the server response and response back, telling the browser, you should go to B. This time IE can see the address has changed, but the history of the back button are bright. Redirection can access resources outside their own web applications. In the redirection process, the transmission of information will be lost. Is an internal server forwards the request to the discretion a Request / Response, and handed over to another for clients, it only knows that his own A first request, without knowing the middle of B, or even C, D. Transmission of information is not lost.

Explain the three common

Suppose you go to apply for a license, 

Redirect: You go to the office A, A person's bureau said that things are not under our control, to B Bureau. Then, you back out from A, B own bus to the office. 

Forward: You go to the office A, A bureau looked after, should know the fact of the matter B Bureau to manage, but he did not return to you, but to make you sit for a while, back to their own people contacted the office of B, let them run after sent over.

The main difference

1. request at different times. Redirect the browser sends a request to the server and receives the response request once again to a new address, is forwarded to the server receives the request completion response to jump to a new address; at least two redirection request, forwarding the request once.

2. different address bar. Redirect the address bar will change the forwarding address bar does not change.

3. whether to share different data. Redirect two requests do not share data, forwarding a request to share data (using the information shared in the Request level, the redirection inevitable mistakes). This means that the forwarding of information transmission is not lost between 2 hops, and redirect transmission of information will be lost between 2 hops (Request range).

4. The different Jump limits. Redirect can jump to any URL, forwarding can only jump site resources.

The different behavior. Redirect client behavior is, the forwarding server-side behavior.

Jump method of using two scenarios

1. Forwarding may be characterized using domain object request is to write data stored therein by the source component.

2. redirection allows users to access the target resource is stored in WEB-INF directory.

3. redirect forwarded faster than slower, because the browser will have to issue a new request, if it is recommended when using forwarding and redirection of use does not matter forward.

4. Because forward only access to the current WEB applications, so access between different WEB applications, in particular, to access to the resource on another WEB site, this time can only use the redirect.

The reason the proportion of directional fast forward, because redirect the client to go through, and there is no forwarding. But if you need to redirect to another external website, you can not use forwarders. In addition, there is a redirection scenarios: avoid calling the same action twice when the user reload the page. For example, when submitting product form, the method of execution saved it will be called, and perform the appropriate action. This is a real application, it is likely that all forms of product information added to the database. But if, after the form is submitted, reload the page to save the method of execution is likely to be called again. The same products will likely again be added. To avoid this situation, after submitting the form, you can redirect the user to a different page, this is the case, any reload this page are no side effects.

However, the redirection is not easy to place, use it can not pass easily to the value of the target page. The use of forwarding, you can simply add a property to the Model, so that the target view can easily access. Because of redirects through the client, so the Model of all will be lost on redirect. Fortunately, after Spring3.1 version of Flash we can attribute to address the loss of traditional values ​​redirect problem.

To use Flash attribute, you must add a <annotation-driven /> in Spring MVC configuration file. Then, you must add a new parameter types on the method: org.springframework.web.servlet.mvc.support.RedirectAttributes.

@RequestMapping (value = "/ Product", Method = RequestMethod.POST)
 public String saveProduct (the ProductForm ProductForm, RedirectAttributes redirectAttributes) {
     // perform business logic stored products 

    // pass parameters 
    redirectAttributes.addFlashAttribute ( "message", "The IS successfully saved Product " ); 

    // perform redirection 
    return " the redirect: / ...... " ; 
}

 

"Maybe the world really worth much to say things, but if you want me to say, I am willing to listen."

Guess you like

Origin www.cnblogs.com/yanggb/p/11825019.html