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.