request.getRequestDispatcher () and response.sendRedirect () difference request.getRequestDispatcher () and response.sendRedirect () difference

request.getRequestDispatcher () and response.sendRedirect () difference

一.request.getRequestDispatcher().forward(request,response):

1, is forwarded, the server is a jump, call corresponds to the method, during the execution of the steering is performed in the current file object file, two files (object file and the current file) belonging to the same request, a front page share request, by this session to pass some data or information, request.setAttribute () and request.getAttribute ().

2, before and after the two executions, the address bar unchanged at the current address file.

3, can not turn to pages and sites outside this web application, so turning faster.

4, URL contained in the "/" indicates the path to the application (project) is.

 

二.response.sendRedirect():

1, belongs to redirect the client also jump, equivalent after client sends a request to the server, also the service sends a request after the server returns a response, the client receives the response, requesting a total of two times, before and after the page do not share a request, the attribute value can not be read by request.setAttribute () provided front steering.

2, before and after the two executions, the address bar changes, is the address of the destination file.

3, can turn to pages and sites outside this web application, the steering speed is relatively slow.

4, URL species contained "/" indicates the path to the root directory.

 

Special applications: data can be modified, deleted, added when operating, should response.sendRedirect (). If you are using request.getRequestDispatcher (). Forward (request, response), then the address bar before and after the operation will not change, remains a modified controller, if this time to refresh the current page, it would resend a request to modify the data, which is to refresh the page in some people it is a reason for the increase of data.

 

How to use the second method to transfer data:

1, you can select the session, but you want to delete the second file;

2, can bring in a request url parameters, as "edit.html? Id = 11"

一.request.getRequestDispatcher().forward(request,response):

1, is forwarded, the server is a jump, call corresponds to the method, during the execution of the steering is performed in the current file object file, two files (object file and the current file) belonging to the same request, a front page share request, by this session to pass some data or information, request.setAttribute () and request.getAttribute ().

2, before and after the two executions, the address bar unchanged at the current address file.

3, can not turn to pages and sites outside this web application, so turning faster.

4, URL contained in the "/" indicates the path to the application (project) is.

 

二.response.sendRedirect():

1, belongs to redirect the client also jump, equivalent after client sends a request to the server, also the service sends a request after the server returns a response, the client receives the response, requesting a total of two times, before and after the page do not share a request, the attribute value can not be read by request.setAttribute () provided front steering.

2, before and after the two executions, the address bar changes, is the address of the destination file.

3, can turn to pages and sites outside this web application, the steering speed is relatively slow.

4, URL species contained "/" indicates the path to the root directory.

 

Special applications: data can be modified, deleted, added when operating, should response.sendRedirect (). If you are using request.getRequestDispatcher (). Forward (request, response), then the address bar before and after the operation will not change, remains a modified controller, if this time to refresh the current page, it would resend a request to modify the data, which is to refresh the page in some people it is a reason for the increase of data.

 

How to use the second method to transfer data:

1, you can select the session, but you want to delete the second file;

2, can bring in a request url parameters, as "edit.html? Id = 11"

Guess you like

Origin www.cnblogs.com/bedbedboy/p/12113125.html