The difference between page jumps in jsp and forward

  • 2020-05-17 06:08:39
  • OfStack

Jumping between pages can be done in two ways: forward and sendRedirect.

forward: can be used in the JSP page, can be implemented in Servlet.

Used on the JSP page < jsp:forward page=" target file "/ > For example, to jump to userlist.jsp, write:

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

To use RequestDispatcher's forward method in Servlet, jump to userlist.jsp by writing:

 
RequestDispatcher rd = request.getRequestDispatcher("userlist.jsp");
rd.forward(request,response);

The sendRedirect method USES the sendRedirect method of the response object. The code is as follows:
 
response.sendRedirect("userist.jsp");

The differences are as follows:

1, the request times are different, this is the most essential difference. In the forward mode, the object corresponding to the target file is called in the process of executing the current JSP object or Servlet object, which is equivalent to method call. request and response objects are passed as parameters to the object corresponding to the target file. The execution of the current file and target file is completed in one request sent by the user. In redirect mode, it is used to request the current file first, the current file returns the address of the target file to the client, the client sends the request again, requests the target file, in fact, sends the request twice.

2. Different methods of value transfer. In forward mode, the current file and the target file belong to the same request and share the request object, so the request object can be used to pass the value. In the redirect mode, the current file and the target file are separate requests, and each request creates a separate request and response object, so that the request object cannot be used to pass the value. In MVC mode, you usually call the model in the controller to get the data, then save it to request, then forward to the target file, which gets the required information from request. If you're using sendRedirect to pass information between the controller and the view, you need to use "? Name = value ".

3. The client sees a different address in the address bar. For forward, it sees the name of the first file in the address bar, and for sendRedirect, it sees the address of the second file in the address bar. For example, the current file is a.jsp in the aa folder, and the target file is b.jsp in the bb folder. To access an image in b.jsp, use the relative path and directly write face.jpg, which is placed in 1 with b.jsp. If you use forward, a.jsp in the address bar, then the system will look for face.jpg in the aa folder, which will cause an error.

The following is a supplement:

1, response jump:

// jump with session, without request (client jump)
responst.sendRedirect (" destination page.jsp");

2, forward jump:

// jump with session, jump with request (server-side jump)
// you need to add parameters to request: request.setAttribute ("myVar", "value");
// get parameters in the destination page: String myVar = request. getAttribute("myVar") == null ? "" : (String) request getAttribute (" myVar");
request. getRequestDispatcher(" destination page.jsp").forward(request, response);

1. forward jump: < jsp:forward page="addInput.jsp" > < /jsp:forward >

a. Server side jump, address bar not changed;
b. Jump to the jump statement immediately without any conditions, and the subsequent code will not be executed (1 must release all resources before the jump);
The properties set by c.request are still available on the page after the jump;
d. Use pass parameters.

2. response jump:

a. Client jump, address bar change;
b. Jump after all code execution;
c. The page after the jump cannot use the request property of the previous page;
d. Overwrite the pass parameter using the address (response. sendRedirect("URL? Parameter name = parameter value ")).

The difference between the two jumps in jsp

There are two types of jumps in jsp, the client side jump and the server side jump, and there are four differences between them:

1. As a server-side jump, the address bar address after the jump is the current address, not the destination page. response.sensRediresct (""), as the client's jump address bar, becomes the address of the destination page.
2. Client jump when the program is executed to this sentence, is all the code after the completion of the execution of the jump action, that is to say, the subsequent code has the opportunity to be executed, and the server-side jump is tough, the execution to this sentence, forced jump, do not execute the subsequent code.
3. From the perspective of passing parameters, the customer service side jump can pass parameters through address rewriting, such as

response. sendRedirect (*. jsp & # 63; ref=pokoo&ref2=pokoo2). The server jump can be used to pass parameters.

4. In jsp, there are four property saving scopes. If the property is saved in request in the current resource, then the client cannot get the contents of the request container when it jumps to the destination page, while the server can get them.

The following is my understanding of the four property saving ranges in jsp and the nine built-in objects in jsp, the jstl tag library, and the simple jsp tag development
Could not be found in servlet, that server jump is used

RequestDispatcher rd=request.getRequestDispatcher("*.jsp");

rd.forward(request,response);


Related articles: