Redirection and forwarding in a servlet (JSP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.