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:
<jsp:forward page= "userlist.jsp"/>
To use the RequestDispatcher forward method in the servlet, to jump to userlist.jsp, you can write:
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:
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 the after 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.