Connection and difference between redirection and redirection

Source: Internet
Author: User
Definition:
Do not use the session scope just to pass the variable to the next page. It will increase the scope of the variable without reason. Forwarding may help you solve this problem.
Redirection: all variables stored in previous requests are invalid and enter a new request scope.
Forward: the variables stored in the previous request will not expire, Just Like splicing the two pages together.

The call method is as follows:
Request. getrequestdispatcher ("apage. jsp"). Forward (request, response); // forward the request to apage. jsp without changing the address bar.
Response. sendredirect ("apage. jsp"); // redirect to apage. jsp. The address in the address bar changes accordingly.
On the JSP page, you will also see the following method for forwarding:

Summary:
Using redirection and forwarding is not a habit. But under what circumstances it must be used.
Do not use the session scope just to pass the variable to the next page. It will increase the scope of the variable without reason. Forwarding may help you solve this problem.
Redirection: all variables stored in previous requests are invalid and enter a new request scope.
Forward: the variables stored in the previous request will not expire, Just Like splicing the two pages together.

1. Dispatch is still the original request, but redirect is to re-create a request.
2. Dispatch is basically the resources forwarded to the context, and redirect can be redirected to external resources, such as: Req. sendredriect ("http://www.mocuai.com ");
The redirection Technology of JSP/servlet is summarized as follows [Supplement]

1. requestdispatcher. Forward ()
Is used on the server. When forward () is used, the servlet engine transmits the HTTP request from the current servlet or JSP to another servlet, JSP or common HTML file, that is, your form is submitted to. JSP, in. JSP uses forward () to redirect to B. JSP. At this time, all information submitted by form is in B. JSP can be obtained, and parameters are automatically transmitted.
However, forward () cannot be redirected to a JSP file with a frame. It can be redirected to an HTML file with a frame. At the same time, forward () cannot be passed with parameters after it, such as Servlet? Name = Frank. No, you can.ProgramThrough response. setattribute ("name", name) to pass to the next page.

The URL in the browser address bar remains unchanged after redirection.

For example, redirection in Servlet
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{

Response. setcontenttype ("text/html; charset = gb2312 ");

Servletcontext SC = getservletcontext ();

Requestdispatcher RD = NULL;

RD = SC. getrequestdispatcher ("/index. jsp"); // targeted page

Rd. Forward (request, response );

}
Usually used in servlet, not in JSP.

2. response. sendredirect ()
It works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank is uploaded to the next page, and can be redirected to different hosts. sendredirect () can be used to redirect JSP files with frame.
After redirection, the URL of the redirection page will appear in the address bar of the browser.
For example, redirection in Servlet
Public void dopost (httpservletrequest request, httpservletresponse response)

Throws servletexception, ioexception

{

Response. setcontenttype ("text/html; charset = gb2312 ");

Response. sendredirect ("/index. jsp ");

}
Because response is an implicit object in the JSP page, you can use response. sendredirect () in the JSP page to directly implement relocation.
Note:
(1) When response. sendredirect is used, no HTML output is available.
This is not absolute. The absence of HTML output means that HTML cannot be sent to a browser. In fact, the current server has a cache mechanism, generally at 8 K (I mean JSP server), which means that unless you disable the cache or you use out. flush () force refresh. A small amount of HTML output is allowed before sendredirect is used.
(2). response. sendredirect should be followed by a return statement;
We already know that response. sendredirect is switched through a browser, so there will be actual actions only after page processing is complete. Now that you have to turn, what is the significance of the subsequent output? In addition, the redirection may fail due to the subsequent output.
Comparison:
(1). Request dispatcher. Forward () is the redirection of control in the container, and the address after the redirection is not displayed in the address bar of the client browser;
(2). response. sendredirect () is a complete jump. the browser will get the jump address and resend the request link. In this way, the link address after the jump is displayed in the address bar of the browser.
The former is more efficient. When the former can meet the needs, try to use the requestdispatcher. Forward () method.

Note: in some cases, to jump to a resource on another server, you must use the httpservletresponse. sendrequest () method.

3. <JSP: Forward page = ""/>

The underlying part is implemented by requestdispatcher, so it carries the marks of the requestdispatcher. Forward () method.

If there are a lot of output before <JSP: Forward>, and the previous output will automatically output to the client when the buffer is full, this statement will not work, so pay special attention to this.
Note: The browser address cannot be changed. Refresh will cause repeated submission.

4. Modify the location attribute of the HTTP header to redirect
You can directly modify the address bar to redirect the page.
JSP fileCodeAs follows:

<%
Response. setstatus (httpservletresponse. SC _moved_permanently );
String newlocn = "/newpath/JSA. jsp ";
Response. setheader ("location", newlocn );
%>

5. When a page is displayed in JSP for several seconds, it is automatically redirected to another page.
In the HTML file, the following code:
<Meta http-equiv = "refresh" content = "300; url = target. jsp">
Its meaning: after five minutes, the page automatically becomes target.html. In the code, 300 is the refresh delay time, in seconds. Targer.html: The target page you want to switch to. If this page is set, the page is automatically refreshed.
As shown in the preceding figure, you can use setheader to automatically redirect a page to another page after several seconds.
Key code:
String content = staytime + "; url =" + URL;
Response. setheader ("refresh", content ); 

How to choose:

Requestdispatcher. forward () method and httpservletresponse. the sendredirect () method has the following differences: the former is only the redirection of control in the container, and the address after the redirection is not displayed in the address bar of the client browser; the latter is a complete redirection, the browser will get the jump address and resend the request link. In this way, the link address after the jump is displayed in the address bar of the browser. Therefore, the former is more efficient. When the former can meet the needs, try to use the request dispatcher. Forward () method, and this will also help to hide the actual link. In some cases, for example, to jump to a resource on another server, you must use the httpservletresponse. sendrequest () method.

 

Principle:
1. Forward
This method is a redirection on the server side. The server sends data to the client in the following way: before sending data to the client, the server first outputs the data to the buffer and then sends the data in the buffer to the client. When will the data in the buffer be sent to the client? (1) When the request from the client is processed and all data is output to the buffer, (2) when the buffer is full, (3) the buffer output method out is called in the program. flush () or response. flushbuffer (), Web Container sends data in the buffer to the client.
This redirection method uses the server-side buffer mechanism. before sending the data in the buffer to the client, the original data is not sent, and the execution is redirected to the redirect page to send the data on the redirect page, data on the Redirect call page will be cleared. Note: There are a lot of outputs before <JSP: forword>, and the previous output will automatically output to the client when the buffer is full, so this redirection method will not work.
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = UTF-8 ″);
Servletcontext SC = getservletcontext ();
Requestdispatcher RD = NULL;
RD = SC. getrequestdispatcher ("/index. jsp ");
Rd. Forward (request, response );
}
2. sendredirect
This method is used for redirection on the client. This method is used to issue a redirection command to the browser by modifying the HTTP header (set the status code 302 and sending a new request to the browser, the browser requests the URL specified in the location so that the browser displays the content of the redirected webpage. This method can accept absolute or relative URLs. If the parameter passed to this method is a relative URL, Web Container converts it into an absolute URL before sending it to the client. Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setcontenttype ("text/html; charset = UTF-8 ″);
// Response. sendredirect ("/index. jsp"); the effect is the same as that of the previous forward
Response. sendredirect ("http://www.sohu.com"); // jump to Sohu
}

 

 

Differences:

1. Forward redirection is the redirection of the same web application implemented in the container. Therefore, the forward method can only redirect to one resource in the same web application. After the redirection, the URL in the browser address bar remains unchanged, the sendredirect method can redirect to any URL, because this method is implemented by modifying the HTTP header. There is no restriction on the URL. After the redirection, the URL in the browser's address bar changes.
2. Forward redirection transmits the original HTTP request object (request) from one servlet instance to another instance, while sendredirect is not the same application.
3. Based on the second point, the parameter transmission method is different. The form parameter of forward is passed along, so the parameters of the HTTP request can be obtained in the second instance. Sendredirect can only pass parameters through links, response. sendredirect ("login. jsp? Param1 = ").
4. sendredirect can process relative URLs and automatically convert them into absolute URLs. if the address is relative, there is no '/', the Web iner considers it relative to the current request URI. For example, if it is response. sendredirect ("login. JSP), login will be searched from the URL path of the current servlet. JSP: http: // 127.0.0.1: 8080/test/servlet redirect URL: http: // 127.0.0.1: 8080/test/servlet/login. JSP. If it is response. sendredirect ("/login. JSP), the URL: http: // 127.0.0.1: 8080/login will be searched from the current application path. JSP. Forward cannot process relative paths in this way.
Java
Their differences are:
Response. sendredirect is to send the page redirection command to the client browser. After receiving the request, the browser resends the page request to the Web server. Therefore, after the browser is executed, the URL displays the page after the jump. A jump page can be any URL (either the current server or other servers ).
Requestdispatcher. Forward is directly processed on the server, and the processed information is sent to the browser for display. Therefore, after the processing, the page displayed in the URL is the page before the jump. Send the request and response messages sent on the previous page to the next page at the same time (while response. sendredirect cannot send the request and response information on the previous page to the next page ). Because forward is processed directly on the server, the forward page can only be on the server.

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.