How does Java Webresponse complete the redirection implementation?
Today, I will talk to you about how Java Webresponse completes the process of redirecting. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
one。 Understanding of redirection
> the client sends a request to the server, and the server returns 302 and brings an address to the browser and asks the browser to request this address. This process is redirection.
For example, there are three people who ask B to do something for A, respectively. B is powerless. B gives A the address of C and asks A to ask C to do it. To put it simply: ask for help.
two。 Considerations for redirection
When the server returns 302 to the browser, it also brings an address, which is sent in the form of a response header, and the name of the header must be Location
During the redirection, the browser sent two requests
three。 Example of flowchart
four。 Code example:
Package cn.xxx.Servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class BServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / set location response header response.setHeader ("Location", "/ Demo01/CServlet"); response.setStatus (302); / / send status code}}
Package cn.xxx.Servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CServlet extends HttpServlet {protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println ("CServlet");}}
After reading the above, do you have any further understanding of how Java Webresponse completes the redirection implementation process? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.