The difference between a servlet's forwarding and redirection

Source: Internet
Author: User
Tags root directory

Forwarding (server-side jump): one request

<jsp:forward>

Request.getrequestdispatcher ("new.jsp"). Forward (request, response);

Redirect (client jump): two requests

Response.sendredirect ("new.jsp");

<a href= "new.jsp" >new.jsp</a>

Forwarding redirection:

1. Forwarding is done on the server side, so called server-side jump

Redirection is done on the client, so called a client jump

2. After forwarding, the address bar does not change

After redirection, the address bar will change

3. Forwarding can only be done within the same Web project scope

Redirects can be in the same Web project or redirected to any other place Eg:response.sendRedirect ("http://www.baidu.com");




A detailed explanation of the differences between Java redirection and forwarding


Redirection and forwarding have an important difference: When forwarding is used, the JSP container uses an internal method to invoke the target page, and the new page continues to process the same request, and the browser will not know about the process. In contrast, the redirect means that the first page notifies the browser to send a new page request. Because when you use redirection, the URL that is displayed in the browser becomes the URL of the new page, and when you use forwarding, the URL remains the same. Redirection is slower than forwarding because the browser has to make a new request. At the same time, the object within the request cannot be used after a redirect because a new request has been generated by the redirection method.

How do you choose to redirect or forward it? Forwarding is usually faster and can keep the object within the request, so he is the first choice. However, since the URL in the browser still points to the start page after forwarding, if the current page is overloaded, the start page will be called again. If you do not want to see such a situation, select forward.

The difference between forwarding and redirection

Instead of using the session scope just to pass the variable to the next page, you can increase the scope of the variable without any reason, and forwarding may help you solve the problem.

REDIRECT: the variables stored in the previous request are all invalidated and enter a new request scope.

Forwarding: The variables stored in the previous request are not invalidated, just like putting two of pages together.

Body Start:

First look different, their calls are as follows:

Request.getrequestdispatcher ("apage.jsp"). Forward (request, response);//Forwarding to apage.jsp

Response.sendredirect ("apage.jsp");//Redirect to apage.jsp

In the JSP page you will also see the following way to implement the forwarding:

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

I was learning JSP, the two concepts are very vague, look at other people's examples, is also confused, do not know when to use which. I hope the following commentary will help you.

Referring to forwarding and redirection has to mention the request scope. Many beginners know that when we submit a form, a new request is created. In fact, when we click on a link, we also create a new request. So how big is the effect of a request? For example: In page a.jsp there is a link <a href= "b.jsp?id=1" > This is a link to B, and also with a parameter </a>. When we click on this connection, a request is made, and for the sake of clarity we call it requesta->b. 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. More complicated, we add the following statement to the B.JSP page:

Request.setattribute ("name", "Funcreal");

Out.println (Request.getattriblute ("name"));//successfully displays the value of the name variable.

Now add a link to the b.jsp: <a href= "c.jsp?age=23" > This is a link to C, but also with a parameter </a> When we click on this connection, a new request is generated, Then Requesta-b also rested, and the new request was called Requestb-c. In the same way, in c.jsp, the variables we can access are only age, because id,name both of these variables belong to Requesta-b, when he does not exist. Here is the source code:
a.jsp
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<body bgcolor= "#ffffff" >
<a href= "b.jsp?id=1" > points to b.jsp, but also with a parameter id=1. Requesta-b is now born </a>
</body>

b.jsp
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<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 is over. Pointing to the c.jsp, and also with a parameter age=23</a>
</body>

c.jsp
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<body bgcolor= "#ffffff" >
<%
Out.println ("id=" + request.getparameter ("id"));
Out.println ("name=" + request.getattribute ("name"));
Out.println ("age=" + request.getparameter ("age"));
%>
</body>

So what's going on with the forwarding? Now add a page called d.jsp, and add a sentence <jsp:forward page= "d.jsp" in c.jsp </body> front/>
d.jsp
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<body bgcolor= "#ffffff" >
Requestb-c's claws have reached the d.jsp page.
<%
Out.println ("age=" + request.getparameter ("age"));
%>
</body>

Run the program, you will find that the contents of the C page is not displayed, because forward is automatically executed, although the address bar is c.jsp but in fact, but the browser is displayed is already d.jsp content, and see the parameters passed from the b.jsp. You can simply understand: forwarding, is to extend the scope of the requestb-c, <jsp:forwardpage= "d.jsp"/>, this sentence is actually the c.jsp and d.jsp glued together, they are like in a 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 that we set in this action will be used in the next page (perhaps another action), it is forwarded.

Below isHttpservletresponse.sendredirectMethod implements the request redirection with theRequestdispatcher.forwardSummary comparison of request forwarding by method implementation:
(1) The Requestdispatcher.forward method can only forward requests to components in the same Web application, and the Httpservletresponse.sendredirect method is not only redirected to other resources in the current application, You can also redirect resources to other applications on the same site, even resources that are redirected to other sites by using an absolute URL. If the relative URL passed to the Httpservletresponse.sendredirect method begins with "/", it is relative to the root of the entire Web site, and if the relative URL specified when the RequestDispatcher object is created starts with "/". It is relative to the root directory of the current Web application.
(2) After the access process that calls the Httpservletresponse.sendredirect method redirects, the URL that is displayed in the browser address bar changes, the initial URL address becomes the target URL of the redirect, and the Calling The browser address bar keeps the initial URL address intact after the request forwarding process for the Requestdispatcher.forward method ends.
(3) The Httpservletresponse.sendredirect method responds directly to the browser's request, and the result of the response is to tell the browser to re-issue the access request to another URL.
For example, the redirection process is like having a nickname called "Browser" who wrote to find Zhang San to borrow money, Zhang San replied that there is no money, let "browser" to find John Doe borrow, and will John Doe now the address of the communication to the "browser." So, "browser" also press Zhang San to provide correspondence address to John Doe letter to borrow money, John Doe received the letter to the money to the "browser". As can be seen, the browser sent out two letters and received two replies, and the browser also knew that the money he borrowed was from John Doe's hand. The Requestdispatcher.forward method forwards the request to another resource on the server side, and the browser only knows that the request has been made and the result has been answered, and does not know that there is a forwarding behavior inside the server program. This process is like nicknamed "Browser" people write to find Zhang San borrow money, Zhang San no money, so Zhang San find John Doe borrowed some money, even can add some of their own money, and then remit the money to the "browser." As can be seen, the "browser" only sent a letter and received a reply, he only knew from Zhang San to borrow money, do not know that a part of the money from John Doe hand.
(4) The caller and the callee of the Requestdispatcher.forward method share the same request object and the response object, which belong to the same access request and response process Whereas the Httpservletresponse.sendredirect method caller and the callee use their own request object and the response object, they belong to two separate access request and response procedures.
For jumps between internal resources of the same Web application, especially before the jump, you need to pre-preprocess the request and pass the preprocessing results using the Httpservletrequest.setattribute method. Then you should use the Requestdispatcher.forward method.
Redirection between different Web applications, especially if you are redirecting resources to another Web site, should make the Httpservletresponse.sendredirect method.
(5) Neither the Requestdispatcher.forward method nor the Httpservletresponse.sendredirect method can have the content actually output to the client until they are called. If something is already in the buffer, the content will be purged from the buffer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.