The difference between forward and redirect request and their implementation?

1. Request and forward is what?

A: The request is forwarded RequestDispatcher.forword method; the role is: inside the server forwards the request to another resource, the browser only know the result of a request and get a response, but I do not know happened forwarding behavior inside the server program.

For example: alias "browser" in a letter to find seating 100 people, seating only 50, so Joe Smith to find John Doe borrowed 50, then the money will be transferred to "browser." Found that "Browser" only issue a letter and received a reply, he only knows that money is lent him Joe Smith, John Doe does not know there is a part of.

2. What is Redirection?

A: Redirection is HttpServletResponse.sendRedirect method; effect: a direct response to the request made by the browser, the result of the response is to tell the browser to re-access request to another URL.

For example: alias "Browser" to borrow a letter to Joe Smith, Joe Smith replied that no money, so that "Browser" to borrow money to find John Doe, John Doe mailing address and told the "browser"; then "Browse "on the information provided by Joe Smith wrote to borrow money to John Doe, John Doe transferred money to the receipt of the letter" browser. " Seen, "browser" made a total of two letters and received two replies, "browser" knows lend money to him is John Doe.

3. Request the difference between forward and redirect?

A: can be divided into five major differences:

3.1 application and the target directory

Request forwarding method can only forward the request to the component with a WEB application; and redirect method can be redirected to other resources in the current application, you can also redirect to other applications on the same site resources and even use an absolute URL to redirect resources to other sites.

If you pass a relative URL redirection methods start with "/", which is the relative root directory of the entire WEB site; if the object is to create a request to forward the specified relative URL begins with "/", it is relatively root of the current application's WEB table of Contents.

3.2 browser address

After the call request forwarding method forwards the request to access process, the browser address bar to keep the original URL address change; and after the call redirect method to redirect the process of access, URL in the browser address bar will change from the initial URL becomes the target URL redirection.

3.3 request response process

They share the same request and response objects between the caller and the callee request forwarding method, which belong to the same request and response process. Redirect the caller and the callee method is used each request and response objects, they belong to two separate access request and response process.

3.4 usage scenarios

For a jump between the same internal resources WEN application, in particular, to the request before jumping some preliminary pretreatment, and to pass the results of the pretreatment method used HttpServletRequest.setAttribute, the request is forwarded by the method; for different redirection between the WEB application, in particular, to be redirected to another resource on the WEB site, the redirection method should be used.

3.5 Client

Whether it is requested to forward or redirect, before they are called, it can not have been the actual output content to the client. If the buffer zone have been some content, the content will be cleared from the buffer.

3.6 implementation

3.6.1 in the servlet request call forwarding, redirection statement is as follows:

request.getRequestDispatcher ( "new.jsp") forword (request, response);. // request to New.jsp

response.sendRedirect ( "new.jsp"); // redirect to new.jsp

3.6.2 In the jsp page to call forward the request, redirect statement is as follows;

<Jsp: forword page = "new.jsp" /> // request to New.jsp

<% Response.sendRedirect ( "new.jsp");%> // redirected to New.jsp

 

 

Guess you like

Origin www.cnblogs.com/su-chu-zhi-151/p/11875641.html