The difference between forward and redirect in JSP

Source: Internet
Author: User

First, the method of invocation

We know that the statements in the servlet that call forward, redirect are as follows:

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

Response.sendredirect ("new.jsp"); Redirect to New.jsp

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

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

Of course, you can also implement redirects in the JSP page:

<%response.sendredirect ("new.jsp"); %>//Redirect to new.jsp

Second, the essential difference

Explain a

In a word, forwarding is the server behavior and redirection is the client behavior . Why do you say that, it depends on the workflow of two actions:

Forwarding process: The client browser sends an HTTP request-the Web server accepts this request-a method inside the container to complete the request processing and forwarding actions within the containers-the target resource is sent to the customer; here, the forwarded path must be a URL under the same Web container. It cannot be turned to other web paths, and the middle passes through the request within its own container. In the customer browser path bar is still the path of its first access, that is, the customer does not feel that the server is forwarded. The forwarding behavior is that the browser makes only one access request.

REDIRECT process: The client browser sends an HTTP request--"The Web server accepts a 302 status code response and corresponds to a new location to the client's browser--" The client browser finds that the 302 response, then automatically sends a new HTTP request, The request URL is the new location address-the server looks for resources based on this request and sends them to the customer. Here the location can be redirected to any URL, since the browser has re-issued the request, there is no concept of request delivery. The client browser path bar displays its redirected path, and the customer can observe the change of address. The redirect behavior is that the browser has made at least two access requests.

Explanation Two

Redirect, in fact, is two times request

For the first time, the client request A, the server responds, and response back, telling the browser that you should go to B. This time, IE can see the address has changed, and the history of the fallback button is also lit. Redirects can access resources other than their own web apps. During the redirection process, the transmitted information is lost.

Example:

Response.sendredirect ("loginsuccess.jsp");

Request forwarding is the processing power of a request/response within the server, handed over to another

For the client, it knows only the one that was requested first, not the middle B, or even C, D. The transmitted information is not lost.

Example:

RequestDispatcher dis=request.getrequestdispatcher ("loginsuccess.jsp");

Dis.forward (Request,response);

Explain three

Let's say you go to a license.

Redirect: You go to a bureau first, the people of a bureau said: "This matter does not belong to our management, to the B-Bureau", then, you have to withdraw from a, you take the bus to the B bureau.

Forwarding: You first went to a bureau, a after watching, know that this thing should actually B to tube, but he did not put you back, but let you sit for a while, oneself to the back office contact B's people, let them do well, sent over.

http://blog.163.com/tsing_hua/blog/static/139622224201101110836644/

http://blog.csdn.net/cuiweibing/article/details/1791655

The difference between forward and redirect in JSP (RPM)

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.