Servlet & Jsp dynamic website development_5. Forwarding & Redirection

1. What is forwarding/redirection

Let’s first talk about what forwarding and redirecting are

Forwarding: Forwarding refers to user A requesting a page through Google Chrome. After the page gets the request, it will request our servlet. The servlet will process the request after receiving the request, and return to our page after processing

Redirection: The previous one is still the same. After the servlet receives the request, it finds that it can't do it. What should I do? It will tell the browser that you can request another request, it can do it, and then our browser will request a new address, then proceed to get the value and display it on the page

 

In these two operations, the forwarding interacted with the server only once, and the redirected request twice. Let's go directly to the picture!

Second, the realization of forwarding

We create a Servlet called ForwardServlet, and then we forward it in doPost, where we need to carry certain parameters for forwarding

Then we add a page in WebContent, the name is forward.jsp, and configure web.xml by the way

Values ​​can be obtained by scope in forward.jsp

Let's start the server and try it~~~

No errors, then request 

Then these values ​​are obtained. This is our forwarding. By the way, looking at the address again, we can see that what our address is requesting, what is it, has not changed.

Third, the realization of redirection

We are getting a class called RedirectServlet, which also needs to inherit from HttpServlet

We will directly request http://localhost:8080/HelloWorld/redirect at that time, let’s see if we can request

Um, there is no way to capture the dynamic picture. In fact, I requested xxx/redirect, and then it redirected directly, and then redirected the request to the xxx/forword request. If you don’t want to wash it, we can take a look at Network

As you can see, the Status status is 302, 302 means redirection, and 200 means normal~

Then we will transform again, move the method in forwordServlet to redirect

Then we restart the project, and then request the xxx/redirect address

As you can see, our request did not get any value. This is because the data is lost by the redirection. The session can be because in a session, the application can be because it is stored in the project~~~

 

You can check it out for yourself, if you don’t understand, please contact me QQ: 2100363119

Welcome everyone to visit my website: https://www.lemon1234.com

If you can, pay attention to my official account, it is on my website, updated every day~~~, unlimited resources to enjoy Java, thank you~

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/112194029