Comparison between response. sendredirect () and requestdispatcher (). Forward in servlet JSP

Source: Internet
Author: User

From: http://blog.csdn.net/JavaEETeacher/archive/2009/06/23/4291523.aspx

 

In Java Web development, when the MVC mode is used, the interface will be selected to respond to the user after the Controller completes the model call. there are usually two methods:

1. Use the sendredirect method of the response object.

2. Use the forward method of the requestdispatcher object.

For example, to jump to the logon interface login. jsp, you can use the following two methods:

1. Use response

Response. sendredirect ("login. jsp ");

2. Use requestdispatcher

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

Rd. Forward (request, response );

What are the differences between the two methods? Under what circumstances should I use?

First, we will introduce the differences between the two.

The first difference is that the process is different: the first method is equivalent to sending a request to the server after the browser receives the response, so it is equivalent to two requests. The second method is equivalent to a method call. When the current file is executed, the system redirects to the target file. Two files (the current file and the target file) belong to the same request, the most essential feature is that the two requests share the reques object and the response object.

The second difference is that the address bar is different. The first method is to display the address of the target file in the address bar of the browser, the second method shows the address of the current file in the address bar of the browser. This is also very important, which will be introduced later.

What should I do when using it?

If the relationship between the two files is very close, use requestdispatcher. If there is no direct association between the two files, use the sendredirect method of response. What kind of relationship is close, and what kind of relationship is not directly related? For example, the relationship between the query controller and the query result interface is close, because the query controller must switch to the query result interface after processing. For example, there is no direct relationship between the Controller that adds information and the query controller. Generally, after adding information, the Controller on the list interface is redirected to the list interface, after the information is modified or deleted, the Controller on the list page may jump to the List page.

The following are two special applications:

1. If you want to pass the information obtained in the Controller to the target file through request. setattribute and request. getattribute, select requestdispatcher. Because the current file and the target file need to share the request object. It is usually used for query.

2. The controller for the function of modifying data (including deleting and adding operations) and the later interface or controller should adopt the response. sendredirect method. If the requestdiapatcher forward method is used, a serious error occurs. Because the address bar is the Controller for modifying information, if the user refresh, it will re-send a request for data modification, which is not the result that the user wants to see. Therefore, when adding a feature, you can add one refresh every time.

How can we use the second method to transmit data? There are two methods:

1. You can select a session, but you must delete it in the second file.

2. You can write it in the request string, such as login. jsp? Info = "the user does not exist! "

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.