In the Javaweb page jumps generally have three kinds of forms, sendredirect,forward and include, what is the difference of three?
I'll explain it first, and then take a small example
First, Sendredirect
How to use
Response.sendredirect ();
The server sends a status code according to logic, tells the browser to request the specified address, and places the required parameters in the forwarding address. Because it is a new application, the original request cannot be read, can be replaced with the session, or use the include, and forward instead
Second, forward
How to use
Request.getrequestdispatcher ("/first.jsp"). Forward (request, response);
The page will be the content of the first.jsp, the address bar does not change
You can use the set properties
Iii. include
How to use
Request.getrequestdispatcher ("/first.jsp"). Include (request, response);
The page will contain both the current page and the first.jsp content.
You can use the set properties
Here is an example
index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >can get ID:<%= Request.getparameter ("id")%></body>
first.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
second.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
third.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
fourth.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Next run the server (using TomCat8.0)
Access project
Click on the first link
The page does not change, but the console has an output indicating that it has been accessed
We tried to pass a parameter to the past id=123456
Cannot get ID, description is a request that the browser re-issued, requests have been destroyed, this is a new ask
Put the flowchart below
Click on a third connection
The page has not changed, but the address bar has changed
Let's pass a parameter in.
Yes, the ID can be displayed, indicating that the same request
Put the flowchart below
Because forward is a server internal jump, will take the request together to the next page, so ID can get
Then click on the Fifth link
The page changes, including the contents of third.jsp and fourth.jsp
Let's pass a parameter.
can get request
Here is the flowchart
I have limited knowledge, and if there are errors, I hope to point out
This article is original, reproduced please declare the source.
About Sendredirect,forward and include differences in Java