Response.sendredirect () and Request.getrequestdispatcher ("/index.jsp"). Forward (request, response)

Source: Internet
Author: User

Knowledge Point Contour
One Discrimination Response.sendredirect () and Request.getrequestdispatcher ("/index.jsp"). Forward (request, response)
Two Analysis of the similarities and differences between request forwarding and redirection " Knowledge Point Summary "

 

The first part:

Both Response.sendredirect () and Request.getrequestdispatcher (). Forward (Request,response) in the JSP can cause the page to jump, but there is a big difference between the two objects. , the following points are in the line:

①response.sendredirect (URL)-----Redirect to the specified URL

Request.getrequestdispatcher (URL). Forward (Request,response)-----Request forwarded to the specified URL

②response.sendredirect (URL)-----is a client jump

Request.getrequestdispatcher (URL). Forward (Request,response)-----is a server-side jump

③response.sendredirect (URL) jumps to the specified URL address, the previous page (the original page before the jump) all end, the original request object will die, the data will disappear. Next, the current new page creates a new request object, which is the result of a fresh request object.

"Detailed process: REDIRECT will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, the server receives the new response to the browser." At this point the page gets a new request from the browser. At this time, in the original jump before the page with Request.setattribute saved things, if in the current new page with Request.getattribute, the resulting will be null. 】

Request.getrequestdispatcher (URL). Forward (Request,response) is the use of request forwarding, in the jump page when the original page with the request and response jump, The request object is always present and is not recreated.

"Detailed process: Forward occurs inside the server, is the browser completely unaware of the case to the browser to another page response. At this time the page received the request is not directly from the browser, may be in the page durable Request.setattribute in the request to put the data, on the page can be transferred directly to the Request.getattribute to obtain data. 】

④ using the Response.sendredirect () URL in the address bar will change

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

⑤ when using Response.sendredirect (), if you need to pass parameters, you can only add parameters after the URL, such as: url?id=1, but not through request or response mode.

Use Request.getrequestdispatcher (). Forward (Request,response) If you need to pass a parameter, you can pass the Response.setattribute within the program ("name", name) to the next page. Instead of passing parameters in the back, such as Servlet?name=frank.

⑥ using the Sendredirect () method allows you to redirect to any URL, and the forward () method can only redirect to a resource in the same Web application.

Action= "/uu" in form form, Sendredirect ("/uu"), relative to the server root path. If the server root path is http://localhost:8080/Test commits to Http://localhost:8080/uu, and "/uu" in forward code represents the path relative to the Web App. If the Http://localhost:8080/Test application is submitted to Http://localhost:8080/Test/uu.

⑦ using the Sendredirect () method of the HttpServletResponse interface

Sendredirect () is working on the user's browser, and it can be redirected to a different host, Sendredirect () can redirect the JSP file with the 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") sent to Http://www.brainysoftware.com
Root path: Response.sendredirect ("/ooo") sent to Http://localhost:8080/ooo
Relative path: Response.sendredirect ("ooo") is sent to Http://localhost:8080/Test/ggg/ooo.


Sendredirect is equivalent to this method:

Response.setstatus (httpservletresponse.sc_moved_permanently);

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

Response.setheader ("Location", NEWLOCN);

⑧ using the Forward () method of the RequestDispatcher interface
Forward () cannot redirect to a frame-capable JSP file and can be redirected to a frame-capable HTML file.

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.

Also note: It can not change the browser address, refresh will lead to repeated submissions.

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

In addition, in addition to these two page jump method, there is a more convenient way:

Meta Refresh Method
This method is provided by HTML, and meta itself is an HTML tag. Use this method:

<meta http-equiv= "Refresh" content= "5; url=http://www.dreamdu.com/"/>
The corresponding Java code is:
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);

Part II:

Difference:

Request Forwarding:

1, request once, the object is unchanged, the address is not changed and the execution efficiency is high (the reason of high efficiency jumps inside the server)

2. Jump inside the station (jump inside the server)

3, can be transmitted value (Request forwarding can carry request parameters)

4,/Root path (Request forwarding plus/, find resources by default to the project root.) such as http://localhost:8080/bjsxt/)

Redirect:

1, request two times, object change, address change and execution efficiency is relatively low (inefficient reason is to jump on the browser side)

2, can station inside station outside

3, cannot carry the request parameter directly, can set the parameter carry manually

4,/Root Path (redirect plus/, find resources by default to the server root)

The purpose of this resource is easy to learn and exchange, the middle may absorb the essence of predecessors, if similar please Haihan.

Response.sendredirect () and Request.getrequestdispatcher ("/index.jsp"). Forward (request, response)

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.