II: Response.sendredirect (Request.getcontextpath () + "/welcome.jsp"); //response.sendredirect () and Request.getrequestdispatcher (). Forward () can all jump, and there are some differences, One point is that the sendredirect () must be Request.getcontextpath () to get the project's directory, or the requested resource is not available.
/*rd = Request.getrequestdispatcher ("/welcome.jsp");
Rd.forward (request, response); * *
The other difference between the two link:http://blog.csdn.net/wunai616568168/article/details/8331151
Forwarding mode: Request.getrequestdispatcher (). Forward ();
REDIRECT mode: Response.sendredirect ();
Below isHttpservletresponse.sendredirectmethod implements the request redirection with theRequestdispatcher.forwardSummary comparison of request forwarding by method implementation:
(1) The Requestdispatcher.forward method can only forward requests to components in the same Web application, and the Httpservletresponse.sendredirect method is not only redirected to other resources in the current application, You can also redirect resources to other applications on the same site, even resources that are redirected to other sites by using an absolute URL. If the relative URL passed to the Httpservletresponse.sendredirect method begins with "/", it is relative to the root of the entire Web site, and if the relative URL specified when the RequestDispatcher object is created starts with "/". It is relative to the root directory of the current Web application.
(2) After the access process that calls the Httpservletresponse.sendredirect method redirects, the URL displayed in the browser's address bar changes, and the initial URL address becomes the target URL of the redirect, and the call Requestdispatcher.forward The browser address bar keeps the initial URL address intact after the request-forwarding process for the method is completed.
(3) The Httpservletresponse.sendredirect method responds directly to the request of the browser, and the result of the response is to tell the browser to re-issue the request for another URL, a process like a person nicknamed "the browser" who wrote to find Zhang San to borrow money, Zhang San replied that there is no money, let "browser" to find John Doe borrow, and will John Doe now the address of the communication to the "browser." So, "browser" also press Zhang San to provide correspondence address to John Doe letter to borrow money, John Doe received the letter to the money to the "browser". As can be seen, the browser sent out two letters and received two replies, and the browser also knew that the money he borrowed was from John Doe's hand. The Requestdispatcher.forward method forwards the request to another resource on the server side, and the browser only knows that the request has been made and the result has been answered, and does not know that there is a forwarding behavior inside the server program. This process is like nicknamed "Browser" people write to find Zhang San borrow money, Zhang San no money, so Zhang San find John Doe borrowed some money, even can add some of their own money, and then remit the money to the "browser." As can be seen, the "browser" only sent a letter and received a reply, he only knew from Zhang San to borrow money, do not know that a part of the money from John Doe hand.
(4) The caller and the callee of the Requestdispatcher.forward method share the same request object and the response object, which belong to the same access request and response procedure, while the Httpservletresponse.sendredirect method calls And the callee use their respective request objects and response objects, which belong to two separate access request and response procedures. For jumps between internal resources of the same Web application, especially before a jump, some pre-processing of the request And to pass the preprocessing results using the Httpservletrequest.setattribute method, you should use the Requestdispatcher.forward method. Redirection between different Web applications, especially if you are redirecting resources to another Web site, should use the Httpservletresponse.sendredirect method.
(5) Neither the Requestdispatcher.forward method nor the Httpservletresponse.sendredirect method can have the content actually output to the client until they are called. If something is already in the buffer, the content will be purged from the buffer.
How do you choose to redirect or forward? Forwarding is usually faster and can keep the object within the request, so he is the first choice. However, since the URL in the browser still points to the start page after forwarding, if the current page is overloaded, the start page will be called again. If you do not want to see such a situation, select forward.
Instead of using the session scope just to pass the variable to the next page, you can increase the scope of the variable without any reason, and forwarding may help you solve the problem.
REDIRECT: the variables stored in the previous request are all invalidated and enter a new request scope.
Forwarding: The variables stored in the previous request are not invalidated, just like putting two of pages together.
Jump mode
Http://localhost:8080/Test applications
Use the forward method to redirect only one resource in the same Web application. The Sendredirect method allows you to redirect to any URL.
Form form action= "/uu"; Sendredirect ("/uu"), relative to the server root path. such as Http://localhost:8080/Test application (then submitted to HTTP://LOCALHOST:8080/UU);
The "/uu" in the forward code represents the relative path to the Web App. such as Http://localhost:8080/Test application (then submitted to HTTP://LOCALHOST:8080/TEST/UU);
(using the RequestDispatcher interface Forward) method
Forward () cannot redirect to a frame-capable JSP file and can be redirected to a frame-capable HTML file.
At the same time forward () can not be passed with parameters at the back, such as Servlet?name=frank, so it is not possible to pass the program through Response.setattribute ("name", name) to the next page.
The browser address bar URL does not change after redirection.
The forward method can be called only if the client does not have output. If the buffer of the current page is not empty, you must first empty the buffer before calling the forward method.
"/" stands for relative to web app path
RequestDispatcher rd = Request.getrequestdispatcher ("/ooo");
Rd.forward (request, response); Submit to Http://localhost:8080/Test/ooo
RequestDispatcher rd = Getservletcontext (). Getrequestdispatcher ("/ooo");
Rd.forward (request, response); Submit to Http://localhost:8080/Test/ooo
RequestDispatcher Rd =getservletcontext (). Getnameddispatcher ("Testservlet"); (Testservlet for a <servlet-name>)
Rd.forward (request, response); submit to servlet named Testservlet
If there is a lot of output before <jsp:forward>, the previous output has filled the buffer and will automatically output to the client, then this statement will not work, which should be particularly noted.
Also note: It can not change the browser address, refresh will lead to repeated submissions
Forwarding from http://localhost:8080/Test/gw/page.jsp
<jsp:forward page= "otherpage.jsp"/> Translated into Pagecontext.forward ("otherpage.jsp") after the JSP page was parsed;
"/otherpage.jsp" submitted to http://localhost:8080/Test/OtherPage.jsp
"Otherpage.jsp" submitted to http://localhost:8080/Test/gw/OtherPage.jsp
(Sendredirect using the HttpServletResponse interface) method 302
is to work in the user's browser, sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page,
At the same time it can redirect to a different host, Sendredirect () can redirect the JSP file with frame.
Assume that the forwarding code is included in the registered Servlet-url of/ggg/tt;jsp for/ggg/tt.jsp:
Absolute path: Response.sendredirect ("http://www.brainysoftware.com") sent to Http://www.brainysoftware.com
Root path: Response.sendredirect ("/ooo") sent to Http://localhost:8080/ooo
Relative path: Response.sendredirect ("ooo") sent to Http://localhost:8080/Test/ggg/ooo,
Sendredirect is equivalent to this way
Response.setstatus (httpservletresponse.sc_moved_permanently);
String NEWLOCN = "/newpath/jsa.jsp";
Response.setheader ("Location", NEWLOCN);
(Meta Refresh) method 200
This method is provided by HTML, and meta itself is an HTML tag. The method of use is: <meta http-equiv= "Refresh" content= "5; url=http://www.dreamdu.com/"/>
The corresponding Java code
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);
Using the Response.sendredirect () address bar will change
Use the information in the Request.getrequestdispatcher (). Forward (Request,response) address bar to remain unchanged.
REDIRECT will first send a response to the browser, then the browser receives this response and then sends a requeset to the server, and then the server sends a new response to the browser. The request received by the page is a new one from the browser.
Forward occurs inside the server and sends a response to the browser's other page without the browser's knowledge. The request received from the page is not sent directly from the browser and may have been released.
So:
Request.setattribute the things that exist
Only jump through Method 2 to get out of the new page
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Some differences between Sendredirect and Getrequestdispatcher