Response.sendredirect () and Request.getrequestdispatcher (). Forward (Request,response) usage

Source: Internet
Author: User

Response.sendredirect (URL)--redirect to the specified URL
Request.getrequestdispatcher (URL). Forward (Request,response)--Request forwarding to the specified URL

1: The two differences:

Response.sendredirect (URL) jumps to the specified URL address, generating a new request, so the parameters to be passed are only added after the URL, such as:
Url?id=1.
Request.getrequestdispatcher (URL). Forward (Request,response) forwards the request directly to the specified URL, so the request is able to obtain the data of the last request directly, that is, with request forwarding, The request object is always present and is not recreated. Sendredirect () Creates a new request object, so the data in the previous request is lost.

More specifically, this is the case:

REDIRECT will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, and then the server sends a new response to the browser. The request received by the page is a new one from the browser.

Forward occurs inside the server and sends a response to the browser's other page without the browser's knowledge. At this time the page received the request is not directly from the browser, may have durable request.setattribute in the request to put the data. On the Go page you can get data directly from the Request.getattribute.

Jump mode

Http://localhost:8080/Test applications

Use the forward method to redirect only one resource in the same Web application. The Sendredirect method allows you to redirect to any URL.

Form form action= "/uu"; Sendredirect ("/uu"), relative to the server root path. such as Http://localhost:8080/Test application (then submitted to HTTP://LOCALHOST:8080/UU);

The "/uu" in the forward code represents the relative path to the Web App. such as Http://localhost:8080/Test application (then submitted to HTTP://LOCALHOST:8080/TEST/UU);

(using the RequestDispatcher interface Forward) method

Forward () cannot redirect to a frame-capable JSP file and can be redirected to a frame-capable HTML file.

At the same time forward () can not be passed with parameters at the back, such as Servlet?name=frank, so it is not possible to pass the program through Response.setattribute ("name", name) to the next page.

The browser address bar URL does not change after redirection.

The forward method can be called only if the client does not have output. If the buffer of the current page is not empty, you must first empty the buffer before calling the forward method.

"/" stands for relative to web app path

RequestDispatcher rd = Request.getrequestdispatcher ("/ooo");

Rd.forward (request, response); Submit to Http://localhost:8080/Test/ooo

RequestDispatcher rd = Getservletcontext (). Getrequestdispatcher ("/ooo");

Rd.forward (request, response); Submit to Http://localhost:8080/Test/ooo

RequestDispatcher Rd =getservletcontext (). Getnameddispatcher ("Testservlet"); (Testservlet for a <servlet-name>)

Rd.forward (request, response); submit to servlet named Testservlet

If there is a lot of output before <jsp:forward>, the previous output has filled the buffer and will automatically output to the client, then this statement will not work, which should be particularly noted.

Cannot change browser address, refresh will cause duplicate commit

Forwarding from http://localhost:8080/Test/gw/page.jsp

<jsp:forward page= "otherpage.jsp"/> Translated into Pagecontext.forward ("otherpage.jsp") after the JSP page was parsed;

"/otherpage.jsp" submitted to http://localhost:8080/Test/OtherPage.jsp

"Otherpage.jsp" submitted to http://localhost:8080/Test/gw/OtherPage.jsp

(Sendredirect using the HttpServletResponse interface) method 302

is to work in the user's browser, sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page,

At the same time it can redirect to a different host, Sendredirect () can redirect the JSP file with frame.

Assume that the forwarding code is included in the registered Servlet-url of/ggg/tt;jsp for/ggg/tt.jsp:

Absolute path: Response.sendredirect ("http://www.brainysoftware.com

Root path: Response.sendredirect ("/ooo") sent to Http://localhost:8080/ooo

Relative path: Response.sendredirect ("ooo") sent to Http://localhost:8080/Test/ggg/ooo,

Sendredirect is equivalent to this way

Response.setstatus (httpservletresponse.sc_moved_permanently);

String NEWLOCN = "/newpath/jsa.jsp";

Response.setheader ("Location", NEWLOCN);

(Meta Refresh) method 200

This method is provided by HTML, and meta itself is an HTML tag. The method of use is: <meta http-equiv= "Refresh" content= "5; url=http://www.dreamdu.com/"/>

The corresponding Java code

String content=staytime+ "; Url= "+url;

Response.setheader ("REFRESH", content);

--------------------------------------------------------------------------------------------------------------- -----------

Using the Response.sendredirect () address bar will change

Use the information in the Request.getrequestdispatcher (). Forward (Request,response) address bar to remain unchanged.

--------------------------------------------------------------------------------------------------------------- -----------

Request.setattribute the things that exist

Only jump through Method 2 to get out of the new page

--------

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.