First, explain that Request.getrequestdispatcher (string arg0) is meant to be "turned" and is not the same as Response.sendredirect (string arg0) redirection;
1..request.getrequestdispatcher (String arg0)---steering features:
1.1. The URL of the address bar is unchanged, such as: When the servlet--a to the servlet---b, the address bar is still a itself, but the content is actually the content of B.
2..response.sendredirect (String arg0)---redirection features:
2.1. The URL of the address bar will change. For example, when the servlet--a turns to servlet---b, the address bar becomes B .
2.2. The redirect "Sendredirect (String arg0)" is somewhat similar to the "forward ()" approach to the turn: the statements placed behind them are not executed. And response cannot be passed.
--------------------------------------------------------------
-------------------------------------------------
explain forward () and include () two methods
Common denominator:
(1) forward () and include () at the time of execution, the URL is the browser address bar that the address is not changed. Where to turn is the address
as the following example: (Create two servlet A and b)
Difference points:
(1) If you call Forward (), all methods or properties related to the response object will lose its effect. Only request can be turned to the next page.
call the Include (), response and request can be passed to the next page of the steering.
Servlet A:
Public A extends httpservlet{
.... Omit other
doget (httpservletrequest req,httpservletresponse resp) {
PrintWriter pw=response.getwriter (); Pw.write ("a");//Typing "a" on the page
request.
getrequestdispatcher ("B"). Forward (REQ,RESP);//See Results 1
//Use the Include (), what will the result be? See results 2
//request.getrequestdispatcher ("B"). Include (REQ,RESP);
}
.... Omit other
}
Servlet B:
Public B extends httpservlet{
.... Omit other
doget (httpservletrequest req,httpservletresponse resp) {PrintWriter pw=response.getwriter ();p w.write ("B");//typing "b" on the page
}
.... Omit other
}
when I enter directly in the Address bar: localhost:8080/demo02/a--and enter to get results like
result 1:----forward ()
The result: there is only one character in the page: B
but the URL on the address bar is still
localhost:8080/demo02/a
result 2: (guess ...) ---include ()
The result: There are two characters in the page: AB
but the URL on the address bar is still
localhost:8080/demo02/a
explain the problem:
Ⅰ. As the common denominator (1) says, either forward () or include (), the URL of the address bar does not become, because they are all methods of steering.
Ⅱ. As can be seen from the first result, the call to the forward () method loses the delivery of the response object, which, if set with request, can be passed to the next page, as described below, with the include (), this problem does not occur.
------------------------, I'm a gorgeous split-line-------------------
so here's the next argument: Call the Forward () method, can the request object be passed to the next page? Are there any statements that are defined under forward () can be executed?
Servlet A:
Public A extends httpservlet{
.... Omit other
doget (httpservletrequest req,httpservletresponse resp) {
PrintWriter pw=response.getwriter (); Pw.write ("a");//Typing "a" on the page
//Use forward () ...
Request.setattribute ("name", "Zhangsan");
request.getrequestdispatcher ("B"). Forward (req, RESP);//See Results 3
Request.setattribute ("Age",);
//Use the Include (), what will the result be? See results 4
//
Request.setattribute ("name", "Zhangsan");
//request.getrequestdispatcher ("B"). Include (REQ,RESP);
//
Request.setattribute ("Age",);
}
.... Omit other
}
Servlet B:
Public B extends httpservlet{
.... Omit other
doget (httpservletrequest req,httpservletresponse resp) {PrintWriter pw=response.getwriter ();p w.write ("b" + "\ r \ n");//typing "b" pw.write on the page (Request.getattribute ("name") + "\ r \ n "); Tests whether the request object can pass Pw.write (Request.getattribute ("age") + "") through forward (); Test whether the statement written after forward () can be executed
}
.... Omit other
}
result 3:---using forward ()
The result is: The Name property in the request setting is displayed on the page: Zhangsan However, the request property set under the Forward () method is null, the execution of the Forward () The method is a property or method that can pass the request object setting, but the code that is written after the forward () method will not be executed :
Result 4:---use include ();
The result: The page shows the same content as the result 3. However, the cause of the include () and forward () is not the same, but the result is just the same!
So what is the cause of age loss in include ()?
For this reason, first understand how the include () implementation process is?
(---Include is the meaning---)
Now let's talk about the process of include () execution: ()
Procedure 1-3 is normal program execution ... In the 4th step, when the program executes to the include () method, the code for Servlet B is included directly into Servlet a.
that is, the servlet B is merged into a, and the merged code is as follows:
by the way, the include () method also loses the Age property value, because the Age property call is a step earlier than the Age property, that is, age has not been assigned a value, the program is called, and so on, after the program call, age is assigned value .
Therefore, it is concluded that the include () and forward () methods cause age to be empty.
-----> from the sitting is my NetEase blog
Personal understanding of the two methods of Request.getrequestdispatcher () forward ()/include (). Where is wrong, but also trouble to remind you!