Difference between forward sendredirect

Source: Internet
Author: User

Sendredirect & forward
Keywords: sendredirect & FO
1. When sendredirect is enabled, the server first responds to the client's status code (usually 302), telling the client to resend the request to the URL specified in the location header (which can be a relative path. Then the client performs the second request as instructed;
2. When forward is enabled, the operation is performed on the server side, and the server executes the new URL directly. The client is not even informed.
3. because sendredirect is automatically requested by the client, the customer's first request data cannot be saved. On the contrary, in the forward case, the customer's first request data is saved, that is, the original request data can be obtained in the new URL.
4. In the same case, because sendredirect is equivalent to the customer's two requests, the forward efficiency is better than sendredirect.

 

 

 

 

From: http://kevinmro.bokee.com/546919.html

 

 

Forward () means forwarding. Sendredirect is a redirection. The biggest difference between the two is:
After forward () is executed, it is still in the same requestrequest range,
Sendredirect is not in the same request range after execution.

 

1. requestdispatcher. Forward ()

It works on the server. When forward () is used, servletengine 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. If this is not the case, you can use response in the program. setattribute ("name", name) to be passed to the next page (Note: In my understanding, we use parameter records and then set the address of the page in the framework with parameters on the Framework page ). the URL in the browser address bar remains unchanged after redirection. because to complete a business operation, you often need to go through multiple steps. After each step completes the corresponding processing, you can proceed to the next step. For example, business processing is usually processed in servlet, and the processing result is directed to a JSP page for display. This looks similar to the servlet chain function, but there are some differences. A requestdispatcher object can send requests to any server resource, not just another servlet. Note: The forward () method can be called only when the response is not output to the client. If the page cache is not empty, the cache is automatically cleared before redirection. Otherwise, an exception is thrown. For example, 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 "); Rd. forward (request, response );}
 
2. response. sendredirect () works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank is uploaded to the next page. At the same time, it can be redirected to different hosts, and the URL of the redirected page will appear in the address bar of the browser. the httpservletresponse interface defines the sendredirect () method that can be used for redirection. The Code is as follows: public void sendredirect (Java. Lang. string location) throws java. Io. ioexception
This method directs the response to the new URL specified by the parameter location. Location can be an absolute URL, such as response. sendredirect ("http://java.sun.com") or a relative URL. If the location starts with "/", the container considers it relative to the root of the current web application. Otherwise, the container will resolve it to the URL relative to the current request. This redirection method causes the client browser to jump to the request URL. The new URL is displayed in the address bar of the browser, which is similar to the HTTP response header information set above. There are three ways to get requestdispatcher to get the request dispatcher object. 1. The getrequestdispatcher (string path) method of javax. servlet. servletrequest. The path can be a relative path, but the current servlet context cannot be exceeded. If path starts with "/", it is parsed as the root of the current context. 2. The getrequestdispatcher (string path) method of javax. servlet. servletcontext. The path must start with "/" and be relative to the current servlet context. You can call the getcontext (string uripath) of servletcontext to get another servlet context and switch to a server resource link in the external context. 3. Use getnameddispatcher (string name) of javax. servlet. servletcontext to get a Web Resource named name, including Servlet and JSP pages. The Resource Name is specified in the Web Application Deployment description file web. xml. There are slight differences in the use of these three methods. For example, the following is an application configuration file web. xml: <? XML version = "1.0"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.2 // en"
Http://java.sun.com/j2ee/dtds/web-app_2_2.dtd>
<Web-app>
<Servlet>
<Servlet-Name> firstservlet </servlet-Name>
<Servlet-class> org. javaresearch. redirecttest. servletone </servlet-class>
</Servlet>
<Servlet>
<Servlet-Name> secondservlet </servlet-Name>
<Servlet-class> org. javaresearch. redirecttest. servlettwo </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> firstservlet </servlet-Name>
<URL-pattern>/servlet/firstservlet/</url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> secondservlet </servlet-Name>
<URL-pattern>/servlet/secondservlet/</url-pattern>
</Servlet-mapping>
</Web-app> two servlets are defined, named firstservlet and secondservlet respectively. The corresponding classes are org. javaresearch. redirecttest. servletone and org. javaresearch. redirecttest. servlettwo. You can access http: // localhost: 8080/servlet/firstservlet/using the following link in the browser. For example, you can write the following code in firstservlet: requestdispatcher RD = request. getrequestdispatcher ("secondservlet ");
Rd. Forward (request, response); the control is directed to the second servlet. Use the method in step 2 to obtain the requestdispatcher code from the servlet context: requestdispatcher RD = getservletcontext (). getrequest
DISPATCHER ("/servlet/secondservlet ");
Rd. forward (request, response); Use the method in step 3 from the preceding web. the xml configuration file defines two servlets named firstservlet and secondservlet respectively, so we can get the name of dispatcher: requestdispatcher RD = getservletcontext (). getnameddispatcher ("secondservlet ");
Rd. Forward (request, response); you can also redirect to secondservlet. The redirection JSP in the JSP page is compiled into a servlet after parsing, so the above redirection code can also be used in the JSP, and JSP also provides more convenient operations, as shown below: <JSP: Forward page = "nextpage. JSP "/> when the JSP page is executed here, the current processing will be terminated and the control will be handed over to nextpage. JSP. 4. How to Select 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.

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.