The difference between page jumps forward and Sendredirect in JSP _jsp programming

Source: Internet
Author: User

Jumps between pages can be achieved 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 a JSP page, such as to jump to userlist.jsp, to write:

Copy Code code as follows:

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

To use the RequestDispatcher forward method in the servlet, to jump to userlist.jsp, you can write:
Copy Code code as follows:

RequestDispatcher rd = Request.getrequestdispatcher ("userlist.jsp");
Rd.forward (Request,response);

Sendredirect way to use the Response object method Sendredirect method completes, the code is as follows:
Copy Code code as follows:

Response.sendredirect ("userist.jsp");

the difference between the two is as follows
1, the number of requests are different, this is the most essential difference. In forward mode, in the process of executing the current JSP object or Servlet object, the object of the target file is invoked, which is equivalent to the method call, and the request and response objects 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 redirect mode, used to first request the current file, the current file to the target file address returned to the client, the client sent the request again, request the target file, actually sent two requests.

2. Different ways of transmitting value. In forward mode, the current file and destination file belong to the same request, sharing the request object, so you can use the request object to pass values. In redirect mode, the current and target files belong to different requests, and each request creates the request and response objects separately, so that you cannot use the request object to pass values. In MVC mode, the model is usually called in the controller to get the data, then saved to the request, then forward to the target file, and the destination file obtains 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 after the target file plus "? Name = value "is passed in the same 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 1th file name, for Sendredirect, in the address bar to see the address of the 2nd file. Sometimes affects the relative path in the target file, for example, the current file is the a.jsp in the AA folder, the target file is the b.jsp in the BB folder, the b.jsp to access a picture, use the relative path, write face.jpg directly, this file and b.jsp put together. If you use the Forward method, the address bar is a.jsp, so the system will be in the AA folder to find face.jpg, this time there will be an error.

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.