Difference between forward and sendredirect in JSP

Source: Internet
Author: User

Page jumps can be achieved in two ways: Forward and sendredirect.

 

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

Use <JSP: Forward page = "target file"/> On the JSP page. For example, to jump to userlist. jsp, you can write:

<JSP: Forward page = "userlist. jsp"/>

Use the forward method of requestdispatcher In the servlet. To jump to userlist. jsp, you can write:

Requestdispatcher RD = request. getrequestdispatcher ("userlist. jsp ");

Rd. Forward (request, response );

 

The sendredirect method uses the method of the response object to complete the sendredirect method. The Code is as follows:

Response. sendredirect ("userist. jsp ");

 

The differences between the two are as follows:

1. Different requests are the most essential difference. In the forward mode, when the current JSP object or servlet object is executed, the corresponding object of the target file is called, which is equivalent to method call, the request and response objects are passed as parameters to the object corresponding to the target file. The execution of the current file and the target file is completed in a request sent by the user. In the Redirect mode, the request is used to first request the current file. The current file returns the address of the target file to the client, and the client sends the request again to request the target file, actually, two requests are sent.

2. Different value transmission methods. In the forward mode, the current file and the target file belong to the same request and share the request object. Therefore, you can use the request object to pass the value. In the Redirect mode, the current file and the target file belong to different requests. Each request will create a separate request and response object, so that the request object cannot be used to transmit values. In MVC mode, the model is usually called in the Controller to obtain data, then saved to the request, and then forward to the target file. The target file obtains the required information from the request. If you use sendredirect to transmit information between the Controller and the view, you need to add "? Name = value.

3. The client sees different addresses in the address bar. For forward, the name of the 1st files is displayed in the address bar. For sendredirect, in the address bar, we can see the address of the 2nd files. The file is put together with B. jsp. If the format forward is a.jspin in the address bar, the system will find face.jpg In the AA folder, and an error will occur.

Related Article

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.