Servlet internal forwarding and redirection

I learned the internal forwarding and redirection of servlet for two or three days. Today, I took a little time to summarize the internal forwarding and redirection of servlet. Since I am also a beginner, please understand if there is something wrong! And hope that I will correct the private message as soon as possible, hoping to help everyone.


First of all, memorize the grammar of the two first, no matter whether it is used or not, write down the grammar first! ! !
Forward:

httpServletRequest.getRequestDispatcher("资源路径").forward(httpServletRequest, httpServletResponse);

Redirect:

httpServletResponse.sendRedirect("/资源路径");

Purpose:
Both are used to implement page jumps and give the client a response.


Difference:
Although both are used to implement page jumps, they give the client a response. However, there are still big differences. Here I will discuss the principles and differences between the two.

One: Principle

servlet forwarding

1. The schematic diagram of the request is as follows:
Servlet internal forwarding and redirection
2. It can be simply understood that the forwarding is like a person going to borrow money from person A, but A has no money, so A ran to person B to borrow money, and then repaid the borrowed money I lent it to you.

servlet redirection

1. The request schematic diagram is as follows:
Servlet internal forwarding and redirection
2. A simple understanding of redirection is like a person going to borrow money from person A, but A has no money, then A tells you that B has money, and then you go to B's house to find B borrow money

Two: Features

Forward Redirect
Forwarding is server behavior (this does not go through the browser) Redirection is a client behavior. (Browser requests again).
Forwarding means that the browser only makes one access request ) Redirection is when the browser makes at least two visit requests (of course, it can also be redirected multiple times).
Forward the browser address unchanged The address of the directed browser changes.
The information transmitted between forwarding two jumps will not be lost, so data can be transferred through request The information transmitted between the redirection jumps will be lost (request scope)
Forwarding can only forward the request to components in the same WEB application Redirection can point to any resource, including other resources in the current application, resources in other applications on the same site, and resources in other sites

Guess you like

Origin blog.51cto.com/14954398/2543001