Redirection and forwarding in a servlet (JSP)
There are two ways to move from the inside of a servlet (JSP) to another servlet (JSP): Forward and redirect. Forwarding: A Web Component (servlet) that handles unfinished processing to the next Web component, and the forwarded component shares the request object and the response object, thusSharing of current data. Ability to forward using the following method:request.getrequestdispatcher ("New Resource"). Forward (REQ,RESP);
Attention:1. Cannot run Response.getwriter () before forwarding. Close () or flush (), because a browser request can only respond once, running this will respond directly to the browser, and will not do any subsequent forwarding or other actions.2. Before forwarding, assume that the response object has a cache, after forwarding will be emptied, that is, you have the Servleta page run output to the browser statement, but you forward SERVLETB, you will only see SERVLETB in the browser output. --Characteristics of forwarding:1) The forwarded resource path must be within the scope of the Web application2) forward carrying data3) Address bar address does not change redirect: The function of a Web component is completed and the next Web component needs to be run immediately, and redirection can be used. Principle:The server sends a status code of 302 and location to the browser. Once the browser receives it, it immediately sends a request to the address to which it points.
Response.sendredirect ("Resource Path");Equivalent to the following two sentences:Response.setstatus (302);
Response.setheader ("Location", "/servletpro/servlet2");
Redirection Features:1) access to discretionary resources
Resp.sendredirect ("http://www.baidu.com");
2) The Address bar changes after redirection
3) redirection does not carry data note:1) cannot run Out.close () or Out.flush () before redirection;
2) before redirection, assume that the response object has a cache and will be emptied after forwarding.
Redirection and forwarding in a servlet (JSP)