Java redirect and forward the request

Redirect the request forwarded by the difference between:

1, the request is forwarded server behavior; redirect client behavior.
2, the request is forwarded the RequestDispatcher.forward () method; redirection using an HttpServletResponse.sendRedirect ().
3, request forwarding can forward the request to other pages on the same web project; redirection can not only redirect to other pages of the current project, you can also redirect to a page on another site.
4, the request is a forwarded request; multiple requests are redirected. The request is forwarded with a request to respond to the browser page forward by the last page; redirect a request before the end, a new request is issued.
5, the received shared between the forwarder and the forwarder forwards the request to the same request and response objects, they belong to the same access request and a response process; redirection caller and callee using respective request and response objects, they belong to multiple independent access request and response process;
 6, forwarding the request URL address will not change; the redirection URL address bar will change.

    1
    2
    3
    4
    5
    6

Implementation code:

Redirect to index.jsp page:
Response.sendRedirect (request.getContextPath () + "/ index.jsp");

Forwards the request to the index.jsp file:
request.getRequestDispatcher ( "/ index.jsp") Forward (Request, Response);.

    1
    2
    3
    4
    5

In one example, forwarding and redirection request understanding. Suppose you go to apply for a license:

Redirect:
You go to the A, A of the land, who said: "This matter is not under our control, go to B", and then, on the back from the A, B went to his own car.

forwards the request:
You go to the A, A looked after, in fact, know how this thing should be to manage B, but he did not let yourself go to B, but to let you wait
-------- -------------  
author: Qin Zifan  
source: CSDN  
original: https: //blog.csdn.net/qq_40785225/article/details/82947679  
copyright: This article is a blogger original article, Please attach reproduced Bowen link!

Published 442 original articles · won praise 77 · views 1 million +

Guess you like

Origin blog.csdn.net/rnZuoZuo/article/details/91847580