The difference between 302 and Sendredirect in Java

Source: Internet
Author: User

There is a sendredirect function in Java that is used to jump to another page, which is actually a 302 jump, but not exactly the same as 302 jump

Click (here) to collapse or open

    1. Response.sendredirect ("login.jsp");
    2. Not equal to
    3. Response.AddHeader ("Location", "login.jsp");
    4. Response.setstatus (302);
1. CompareResponse.sendredirect ("login.jsp"), the HTTP request process is as follows: Response.AddHeader ("Location", "login.jsp"); Response.setstatus ( 302); The above two comparisons find that the blue part response.sendredirect ("login.jsp") is different, and a http://localhost:8080/xxxSearch/is added to the redirect.2. Analysis1) When the access path of our browser is http://localhost:8080/xxxSearch/, Response.sendredirect ("login.jsp");equalsResponse.AddHeader ("Location", "login.jsp"); Response.setstatus (302); At this point in the JSP use Request.getcontextpath () Getting access to an absolute path is possible 2) but when our browser's access path is http://localhost/, there is a problem like my company is often the structure: using Apache as the front end, the agent behind the Tomcat, in Tomcat it seems to access the path or http:/ /localhost:8080/xxxsearch/, and the user real access is http://localhost/if use Response.sendredirect ("login.jsp"), then the user will jump to http:// localhost:8080/xxxsearch/login.jsp If our firewall does not open port 8080, we will see inaccessible pages, and we do not want customers to directly access Tomcat. And with Response.AddHeader ("Location", "login.jsp"); Response.setstatus (302); Then the client will visit login.jsp, This path is a relative path plus the client's browser path, and the customer actually accesses the http://localhost/login.jsp3. ConclusionSo I think: try to use Response.AddHeader ("Location", "login.jsp"); Response.setstatus (302); And in the JSP page try not to use Request.getcontextpath () or absolute path, so that our code can be very good in different ways to access, especially SEO optimization is very beneficial

The difference between 302 and Sendredirect in Java

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.