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:
Copy codeThe Code is as follows: <jsp: forward page = "userlist. jsp"/>
Use the forward method of RequestDispatcher In the Servlet. To jump to userlist. jsp, you can write:Copy codeThe Code is as follows: 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:Copy codeThe 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.