views:

377

answers:

3

I read about "HTTP persistent connection" but somehow I don't seem to understand what does persistent mean in this context.
Could you'll elaborate?

A: 

In the case of HTTP, it means that if you try to load a website, the connection will stay open after the site is fully loaded, to save resources and such. You can then use that connection again to make any additional requests, such as loading another page.

nasufara
so in my Java servlet if I do something like out.close() which is generally done in the finally block, will the connection still remain open?
Kevin Boyd
I'm not that well versed in Java, but if close() does what I think it does (closes the connection), then no.
nasufara
+4  A: 

It means the server doesn't close the socket once it's finished pushing out the response (so the length of the response has to be otherwise indicated, via headers or chunking), so the client can make other requests on the same socket. A web page often requests several other pieces (images, CSS, scripts, ...) on the same server as the page itself, so reusing the socket for some of those further requests to the same server can reduce overall latency compared to closing the original socket and opening new ones for all the follow-on requests.

Alex Martelli
As darkassassin93 has stated that the connection will die out in case of out.close() on a Servlet. In this case how could we implement HTTP persistent connection on a Java Tomcat server? Or will we have to use 3rd party tools to implement this? Another question is what about the client side?
Kevin Boyd
@Kevin, you have no control over the browser ("client side"): it will establish its own policies (you can just hope it supports "stay alive"; most do). Re calling `.close`, I do believe that closes the connection (that being its specific purpose) so the way to (hopefully, browser cooperating) NOT close the connection is NOT to call `close` -- may not be sufficient but is surely necessary.
Alex Martelli
+1  A: 

All the discussion till now has been from the browser side of things. The browser first request the actual page, and it parses the page and finds out all other resources that it needs before it can render that page. The browser requests these resources and other dependent resources one by one. So maintaining a persistent connection is very efficient here, as the overhead of creating and destroying connections is avoided.

Now from web server side of things, a persistent connection would be one that allows it to "push" content to the web browser. Now HTTP doesn't support this. So, there are few workarounds with javascript where the page is basically refreshed after a while.

You can see this being trick being used by many web based email providers which continuously keep checking in the background for new mails. This gives a feeling that when a new mails arrives, the server "pushes" the new mail notification to the web browser. But in fact, its actually the web browser which keeps on checking the server for any new mail.

Also another point that I would like to state is that we actually don't see any page refresh that's because of another trick which allows only specific parts of the page to be refreshed by the request. (HINT: AJAX)

Trainee4Life
Very Informative answer!
Kevin Boyd
If I keep a conncection open with a webserver will the browser show continuosly a loading bar?
Kevin Boyd
A web browser will only show a loading bar, if it actually requests something. Background stuff whether a connection is open or not shouldn't get reflected in the UI of the browser.
Trainee4Life