Differences between the Sendredirect () and Forward () methods in Java

Source: Internet
Author: User

I. Article 1

Both the Httpservletresponse.sendredirect and Requestdispatcher.forward methods can be implemented to obtain the appropriate URL resources.

Sendredirect implements request redirection, forward implements request forwarding.

The processing mechanism inside the Web server is not the same.

1. Jump mode

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);

2.forward Redirect, the browser URL address is not changed, after Sendredirect forwarding, the browser URL address becomes the destination URL address.

Forward () cannot redirect to a frame JSP file, can be redirected to a frame HTML file, and forward () cannot be passed with parameters at the back.

such as Servlet?name=frank, this is not possible, can be in the program through the Response.setattribute ("name", name) to the next page.

3. Using the forward redirection process, the browser sends the request request to the destination servlet first, then the server driven by the servlet to the destination URL, and the server-side servlet returns response to the browser side. The browser and the server request a response at a time.

Using the Sendredirect forwarding process, the browser first sends a request to the destination servlet, and the servlet sees that Sendredirect returns the destination URL to the browser, the browser requests the destination URL, and the destination URL returns response to the browser. Browser and server two request response.

4. Sharing request and response between the caller of the forward method and the callee

The Sendredirect method has two requests and response because of a two-time browser server request.

If you use Request.setattribute to pass some properties you need to use forward, if you want to jump to another app's resources, you need to use Sendredirect.

5. Either the forward method or the Sendredirect method call cannot have PrintWriter output to the client.

Forward method Error: Java.lang.IllegalStateException:Cannot forward after response have been committed

Sendredirect Error: Java.lang.IllegalStateException
At Org.apache.catalina.connector.ResponseFacade.sendRedirect (responsefacade.java:435)

Two. Article 2

Jumps between pages can be implemented in two ways: forward and sendredirect.

Forward: can be used in JSP pages and can be implemented in a servlet.

Use <jsp:forward page= "target file"/> in JSP pages, for example, to jump to userlist.jsp, you can write:

<page= "userlist.jsp"/>

To use RequestDispatcher's forward method in the servlet, to jump to userlist.jsp, you can write:

The Sendredirect method uses response object methods Sendredirect method completion, the code is as follows:

the difference between the two is as follows :

1, the number of requests is different, this is the most essential difference. In the forward mode, when executing the current JSP object or the Servlet object, the object corresponding to the target file is called, which is equivalent to the method invocation, and the request and the response object are passed as parameters to the object corresponding to the target file. The execution of the current and target files is done in a single request sent by the user. In the redirect mode, used to first request the current file, the current file to the destination file address returned to the client, the client sends the request again, request the target file, actually sent two requests.

2. The method of transmitting value is different. In forward mode, the current and destination files belong to the same request and share the request object, so you can use the request object to pass the value. In redirect mode, the current and destination files belong to different requests, and each request creates a separate request and response object, so that you cannot use the request object to pass values. In MVC mode, the model is typically called in the controller to get the data, then saved to the request, then forward to the target file, and the destination file gets the required information from the request. If you use the Sendredirect method to pass information between the controller and the view, you need to use the after target file to add "? Name = value "is passed in the way.

3, the client in the address bar to see the address is not the same, for forward, in the address bar to see the name of the 1th file, for Sendredirect, in the address bar to see the 2nd file address. Sometimes affect the relative path in the target file, for example, the current file is a a.jsp in the AA folder, the target file is the b.jsp in the BB folder, in the b.jsp to access a picture, using the relative path, directly write face.jpg, this file and b.jsp put together. If you use forward mode, the address bar is a.jsp, so the system will find face.jpg in the AA folder, this time will be an error.

Here are the additions:

1,response Jump:

Jump with session, without request (client jump)
Responst.sendredirect ("Destination page. jsp");

2,forward Jump:

With session, jump with request (server-side jump)
You need to add parameters to the request: Request.setattribute ("MyVar", "value");
Get parameters in the destination page: String MyVar = Request.getattribute ("myVar") = = null? "": (String) Request.getattribute ("MyVar");
Request.getrequestdispatcher ("Destination page. jsp"). Forward (request, response);

1.forward Jump: <jsp:forward page= "addinput.jsp" ></jsp:forward>

A. Server-side jump, the address bar does not change;
B. Execute the jump statement immediately after the unconditional jump, after the code is no longer executed (jump must release all resources);
The properties of the C.request settings can still be used after the jump page;
D. Use pass-through parameters.

2.response Jump:

A. The client jumps, the address bar changes;
B. Jump after all code execution is completed;
C. The page after the jump cannot use the request property of the previous page;
D. Use address rewriting to pass parameters (Response.sendredirect ("URL? parameter name = parameter value")).

The difference between two jumps in JSP

There are two kinds of jumps in JSP, client jump and server side jump, there are 4 points difference between them:

1. As a server-side jump, the address bar address is the current address, not the destination page. and RESPONSE.SENSREDIRESCT ("") as the client jump Address bar will become the address of the destination page.
2. Client jump when the program executes to this sentence, is all code execution after the execution of the jump action, that is, the subsequent code has the opportunity to be executed, and the server side of the jump is tough, execute to this sentence, force jump, do not execute the code behind.
3. From the point of view of passing parameters, the customer service side jumps can pass the parameter through the way of address rewriting, such as

Response.sendredirect (*.JSP?REF=POKOO&REF2=POKOO2). The server's jump can be used to pass parameters.

There are four types of properties in the 4.jsp, such as the current resource in the request to save the properties, then the client's jump to the destination page is not available, the request container of things, and the server-side jump can be taken.

The following is an understanding of the JSP in the four types of properties to save the scope and JSP 9 built-in objects, and the JSTL tag library, and the development of a simple JSP tag
Not available in Servlets, the server jumps with

RequestDispatcher rd=request.getrequestdispatcher ("*.jsp");

Rd.forward (Request,response);

Differences between the Sendredirect () and Forward () methods in Java

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.