Forwarding and redirection process and differences

Fortunately airship has harmed so many people [expert group] + 1892213 give you a different small coup

Forwarding req.getRequestDispatcher ( "/ address to jump") .forward (req, resp);

:( forwarding process to servlet and jsp for example)

The browser first sends a request to the server, the server-side match is found servlet, and assign it to perform, when the servlet executed, it calls getRequestDispacther () method forwards the request to the specified jsp page, the entire process is on the server side, but also inside the same request to complete, so the servlet and jsp share is the same request, everything inside the store in the servlet, jsp pages can be taken out. The whole process is a request, a response.

Error-prone points:

a) does not change the address bar

b) forwarded only be forwarded to resources within the current web application

c) during the forwarding process, the data can be saved to a domain object request (using the same as a forwarding request field)

d) forward is to jump on the server side, the browser did not know

 

Redirect resp.sendRedirect ( "/ address to jump");

:( redirection process to servlet and jsp for example)

The browser sends a request to the server, the server matches the servlet, which forwards the request and have the same call after sendRedirect servlet processed () This method, which is a method of response, so when this servlet processed, see response.senRedirect () method, immediately return the response to the browser (containing the response headers and a new URL address), you have to respond again to tell the browser sends a request to access jsp page, followed by the browser receives the request after once again issued a new request to request jsp page. These two non-interfering request, independently of each other, in front of anything inside request setAttribute () in which the latter request can not be acquired. It is seen, in which two redirection request, in response to two.

Error-prone points:

Address a) the address bar will change into after the redirect

b) Redirect can jump to the current web application or other web applications, even outside the domain site

c) not be in the process of redirection, the save data to the request (when transmitting a second request because the request, the first request of the domain will expire)

d) to redirect the jump in the browser, the browser Informed

 

Forwarding and redirection difference:

Forwarding a jump server side is the browser sends a request to the server, the server will pass intact directly related parameter information request to the other server or servlet jsp to handle. Jump redirected in a browser, the server returns a response to the browser header and a new URL address, the original parameters, or some other information, if the server is not special treatment does not exist, the browser will access the new URL is pointing to the servlet or jsp.

 

Precautions: If you are using domain objects request data sharing, only forward! ! !

Guess you like

Origin www.cnblogs.com/java67/p/11728585.html