The difference between redirection and forwarding in java

response.sendredirect("http://www.foo.com/path/error.html");

There is an important difference between redirection and forwarding: when forwarding is used, the JSP container will use an internal method to call the target page , the new page continues to process the same request, and the browser will not know about the process. In contrast, the meaning of redirection is that the first page notifies the browser to send a new page request. Because, when you use redirection, the URL displayed in the browser becomes the URL of the new page, and when you use forwarding, the URL remains the same. Redirects are slower than forwards because the browser has to make a new request. At the same time, because the redirection method generates a new request, after a redirection, the objects in the request will not be available.

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

The difference between forwarding and redirecting

Don't use session scope just to pass the variable to the next page, it will increase the scope of the variable for no reason, forwarding may help you solve this problem.

Redirection: All variables stored in the previous request are invalid and enter a new request scope.

Forwarding: The variables stored in the previous request will not be invalidated, just like piecing together two pages.

The text begins:

First, they look different, and their calls are as follows:

request.getRequestDispatcher("apage.jsp").forward(request, response);//forward to apage.jsp

response.sendRedirect("apage.jsp");//reload Directing to apage.jsp

In the jsp page, you will also see that forwarding is achieved in the following way:

<jsp:forward page="apage.jsp" />

When I was just learning jsp, I was very vague about these two concepts, see When looking at other people's examples, I am also confused, and I don't know when to use which. Hope the explanation below can help you.

When it comes to forwarding and redirection, we have to mention the request scope. Many beginners know that when we submit a form, a new request is created. Actually, when we click on a link, a new request is also created. So how big is the role of a request? For example: there is a link in page a.jsp <a href="b.jsp?id=1">This is a link to b, and it also takes a parameter</a>. When we click on this link, a request is generated, which we call requestA->B for clarity. Now, in the b.jsp page we can get the information from this request. In b.jsp you can write out.println(request.getParameter("id")) to test. The following is a bit more complicated. We add the following statement to the b.jsp page:

request.setAttribute("name","funcreal");

out.println(request.

Now add another link in b.jsp: <a href="c.jsp?age=23">This is a link to c, and it also takes a parameter</a>, when we click on this link At this time, a new request will be generated, and then requestA-B will rest in peace. The new request is called requestB-C. In the same way, in c.jsp, the only variable we can access is age, because the two variables of id and name belong to requestA-B, and he no longer exists at this time. Here is the source code:
a.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<a href="b.jsp?id=1" >points to b.jsp with a parameter id=1. requestA-B is now born</a>
</body>
</html>

b.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<a href="c.jsp?age=23">requestA-B已经结束了。指向c.jsp,而且还带了一个参数age=23</a>
</body>
</html>

c.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
out.println("name=" + request.getAttribute("name"));
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>

So what about forwarding? Now add a page called d.jsp, and add a sentence <jsp:forward page="d.jsp"/>
d.jsp
<%@ page contentType="text/html; charset in front of </body> in c.jsp =GBK" %>
<html>
<body bgcolor="#ffffff">
requestB-C has reached the d.jsp page
<%
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>

Run the program, you will find that the content in the c page is not displayed, because the forward is executed automatically, although the address bar is c.jsp, but in fact, it is displayed in the browser It is already the content of d.jsp, and I have seen the parameters passed from b.jsp. You can understand it simply like this: forwarding is to extend the scope of requestB-C, <jsp:forwardpage="d.jsp"/>, this sentence actually sticks c.jsp and d.jsp together, They are like in one page.
If you have used struts, then you know why in Action, the last sentence is almost always mapping.findForward("xxx");. Because the request-scoped variables we set in this Action will be used in the next page (maybe another Action), so use forwarding.

The following is a summary comparison of the request redirection implemented by the HttpServletResponse.sendRedirect method and the request forwarding implemented by the RequestDispatcher.forward
method: (1) The RequestDispatcher.forward method can only forward requests to components in the same WEB application; and the HttpServletResponse.sendRedirect method Not only can you redirect to other resources in the current application, but you can also redirect to resources in other applications on the same site, or even redirect to other sites using absolute URLs. If the relative URL passed to the HttpServletResponse.sendRedirect method begins with "/", it is relative to the root directory of the entire WEB site; if the relative URL specified when creating the RequestDispatcher object begins with "/", it is relative to the current WEB application 's root directory.
(2) After the access process redirected by calling the HttpServletResponse.sendRedirect method, the URL displayed in the browser address bar will change, from the initial URL address to the redirected target URL; and the request that calls the RequestDispatcher.forward method is forwarded After the process is over, the browser address bar keeps the original URL address unchanged.
(3) The HttpServletResponse.sendRedirect method directly responds to the browser's request, and the result of the response is to tell the browser to re-send an access request to another URL.
For example: The redirection process is like a person nicknamed "Browser" who wrote to Zhang San to borrow money, Zhang San replied that he had no money, and asked the "Browser" to borrow money from Li Si, and transferred Li Si's current The correspondence address is told to the "browser". Therefore, the "browser" wrote a letter to Li Si to borrow money according to the correspondence address provided by Zhang San, and Li Si remitted the money to the "browser" after receiving the letter. It can be seen that "Browser" has sent a total of two letters and received two replies, and "Browser" also knows that the money he borrowed came from Li Si. The RequestDispatcher.forward method forwards the request to another resource on the server side. The browser only knows that the request has been issued and got the response result, but does not know that the forwarding behavior has occurred inside the server program. This process is like a person nicknamed "Browser" who wrote to Zhang San to borrow money, but Zhang San had no money, so Zhang San asked Li Si to borrow some money, and he could even add some of his own money, and then transfer the money. Sent to the "browser". It can be seen that the "browser" only sent a letter and received a reply. He only knew that he had borrowed money from Zhang San, but he did not know that part of the money came from Li Si.
(4) The caller and the callee of the RequestDispatcher.forward method share the same request object and response object, and they belong to the same access request and response process; while the caller and the callee of the HttpServletResponse.sendRedirect method use their own request object and response object, they belong to two independent access request and response process.
For the jump between the internal resources of the same WEB application, especially before the jump, some pre-processing should be performed on the request, and the HttpServletRequest.setAttribute method should be used to pass the pre-processing result, then the RequestDispatcher.forward method should be used.
Redirection between different WEB applications, especially to redirect to resources on another WEB site, should use the HttpServletResponse.sendRedirect method.
(5) Whether it is the RequestDispatcher.forward method or the HttpServletResponse.sendRedirect method, before calling them, no content has been actually output to the client. If there is already some content in the buffer, that content will be flushed from the buffer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645137&siteId=291194637