Home>Article>Backend Development> Web page forwarding and redirection

Web page forwarding and redirection

jacklove
jacklove Original
2018-06-11 23:42:10 2227browse

In web development, there is a big difference between forwarding and redirection.

Intuitively speaking, forwarding will not change the URL address, while redirection will change the URL.

This is just an appearance.The request object and response object in HttpServlet are encapsulated and generated by the server based on the parameters passed by the browser when the user requests a web page. Once the request is made from the browser and the server responds back, the request and response have reached the end of their lives.

When using forwarding, the browser only requests once but the server may experience multiple jumps. If forwarding occurs during server-side execution, the server will stop the task being executed and specify the task to forward the given address.

If you use redirection, the browser will make multiple requests to the server. When the code is executed on the server side, if a redirection occurs, it will notify the browser to access another URL. The browser will request the resource from the URL sent.

Look at a piece of code:

login
name :

This is a piece of jsp code. When you click submit, the form will be submitted to myServlet.

The interface looks like this:

Web page forwarding and redirection##

public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = (String)request.getParameter("username"); request.setAttribute("welcome", "welcome!!!"); RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp"); rd.forward(request,response); } }

This is MySerlet. When the user clicks to submit the form, the server will call the doPost method of MyServlet. In this What is done in the method is page forwarding.

welcome
<%=request.getParameter("username")%>
<%=request.getAttribute("welcome") %>

This is the code for welcome.jsp.

The submission result is:


Web page forwarding and redirectionAfter clicking the submit button, we can take a look at the network request process.

Web page forwarding and redirection

It can be found that the browser only sent one request to the server.

Next let’s look at redirection.

If you change the code of MyServlet.

public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = (String)request.getParameter("username"); request.setAttribute("welcome", "welcome!!!");// RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");// rd.forward(request,response); response.sendRedirect("welcome.jsp"); } }

You can see that the result is:


Web page forwarding and redirection

And the browser item server sent two requests:


Web page forwarding and redirection

From above The result can be seen that after the redirection, because another request is initiated, the contents of the welcom page are all null. Since the forwarding is the same request object and response object, it can not only obtain the parameters requested by the browser, but also obtain the attribute values put in the request.

In fact, it can be seen from the code that the sendRedirect() method is the response method of the HttpServletResponse object. Since the method of the response object is called, it indicates that the entire request is over, and the server-side item client returns the execution result. The getRequestDispatcher method is a method of the request object, which indicates that the request is still being made, so the result will not be returned to the browser immediately, but will continue to perform its forwarding tasks.

There is one thing that needs to be explained.

Whether it is the URL passed in the forward method or the sendRedirect method, you need to pay attention to it. If it starts with "\", it means that this URL is a request for the root of the servlet container. That is, localhost:8080. If it does not start with "\", it indicates that the request address is addressed relative to the current request URL.

login
name :

When I click submit again:


Web page forwarding and redirection

The request address that does not start with "\" is:


Web page forwarding and redirection

The forwarding model is:


Web page forwarding and redirection

The redirection model is:

Web page forwarding and redirection

This article explains the forwarding and redirection of web pages, and more related content Please pay attention to php Chinese website.

Related recommendations:

Simple PHP MySQL paging class

Two tree array constructors without recursion

Convert HTML to Excel, and realize printing and downloading functions

The above is the detailed content of Web page forwarding and redirection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn