Original article: http://blog.csdn.net/java2000_net/article/details/3002763
1. sendredirect
Usage:
The servlet is the same as the JSP.
- Response. sendredirect ();
Note:
The server sends a status code based on the logic, telling the browser to request the specified address again. Generally, you will put the required parameters in the forwarded address.
Note: The content using request. setattribute cannot be read. You can use session instead, or include or forward instead.
Flowchart
Ii. Include
Usage
Servlet
- Request. getrequestdispatcher ("jsp2.jsp"). Include (request, response );
Inside JSP
- <JSP: Include page = "include. jsp"/>
Description
The page contains the content of page 1 and page 2 at the same time, and the address bar remains unchanged.
The content of request. setattribute can be used normally.
Flowchart
Iii. Forward
Usage
Servlet
- Request. getrequestdispatcher ("jsp2.jsp"). Forward (request, response );
Inside JSP
- <JSP: Forward page = "include. jsp"/>
Description
The page will be the content of page 2, and the address bar will not change
The content of request. setattribute can be used normally.
Flowchart
Special tips
The include () method is used to forward requests to other servlets. The called Servlet's response to this request will be incorporated into the original response object, the original servlet can still output the response information. The forward () method is used to forward the request to other Servlets, And the called servlet is responsible for responding to the request, the execution of the servlet is terminated.
After include and sendrediect, the subsequent statements will continue to be executed, so return should be written in the appropriate position; Forward won't