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
- Response.sendredirect ("login.jsp");
- Not equal to
- Response.AddHeader ("Location", "login.jsp");
- 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