Home >Web Front-end >HTML Tutorial >The difference between forwarding and redirection in web development

The difference between forwarding and redirection in web development

little bottle
little bottleforward
2019-04-29 11:55:374168browse

When doing web development, jumps are the most common. Today I will learn two types of jumps. Friends who are interested can take a look. I hope it will be helpful to you.

The first is request.getRequestDispatcher().forward(request,response):

1. It belongs to forwarding, too Server jump is equivalent to a method call. During the execution of the current file, it switches to the target file. The two files (current file and target file) belong to the same request. The previous and next pages share a request. You can use this to pass some data or session information, request.setAttribute() and request.getAttribute().

2. After two executions, the address bar remains unchanged and is still the address of the current file.

3. You cannot redirect to pages and websites outside this web application, so the redirection speed must be fast.

4. The "/" contained in the URL indicates the path of the application (project).

The second type is response.sendRedirect():

1. It is a redirection and a client jump, which is equivalent to the client After sending a request to the server, the server returns a response. After receiving the response, the client sends another request to the server. There are 2 requests in total. The front and rear pages do not share a request and cannot be read through request.setAttribute() before redirecting. Set the property value.

2. After two executions, the address bar changes to the address of the target file.

3. You can redirect to pages and websites outside this web application, so the redirection speed is relatively slow.

4. The "/" contained in the URL represents the path to the root directory.

Special application: When modifying, deleting, or adding data, response.sendRedirect() should be used. If request.getRequestDispatcher().forward(request,response) is used, the address bar before and after the operation will not change. It is still the modified controller. If the current page is refreshed at this time, it will be resent. A request is made to modify the data, which is why some people add a piece of data after refreshing the page.

How to use the second way to transfer data:

#1. You can choose session, but it must be in the second file Delete;

2. You can bring parameters in the requested url, such as "add.htm?id=122"

How to choose redirection Or forward it? Usually forwarding is faster and can keep the object in the request, so it is the first choice. However, since after forwarding, the URL in the browser still points to the start page, if the current page is reloaded at this time, the start page will be called again. If you don't want to see this happen, choose forwarding.

Related tutorials: Front-end video tutorial

The above is the detailed content of The difference between forwarding and redirection in web development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:html what does it meanNext article:html what does it mean