Search code examples
javahttphttp-redirecthttp-status-code-301

301 redirect: Why connection close?


I learned to use Connection: close when doing 301 redirects in Java

response.setStatus(301);
response.setHeader("Location", "http://www.example.com/");
response.setHeader("Connection", "close");

Why do we do this? Why not omit the last line?

I have seen this in at least three examples, including this one: http://www.pardontheinformation.com/2010/09/java-servlet-jsp-301-and-302-redirect.html

I have never seen the last line omitted.


Solution

  • If your redirection points to a different server, the browser will have to use another connection anyway, so you're just giving the browser advance notice that it probably won't need to contact the current server again for this page. However, if your redirection points to the same server, I see no reason to close the connection.