Hello everyone, I googled and I checked at javaranch, but without any much success... Here is my problem: I have multiple <IMG> tags on one page. Each of them are being served by a servlet. It looks like this: <IMG src="/context/servlet/ImageServlet?imageId=614647"> So, this means, n images corrospond to n TCP connection negotiations and a lot of overhead. "Keep-alive" or "persistent" connection seems to be the solution. I have read the respective chapter of O'Reilly Servlet Programming (no excample) and googled for excamples. The excamples I found quite confused me, because they created the response and set the contentLength before flushing the output stream.
Here are my questions: How does the browser/client know, that he gets the <IMG> content through a keep-alive connection? Does the client still send n requests to the servlet for n images? Does that mean that my servlet has to know the combined length of all n images, and flush the output stream with the n'th image?
The client's browser will handle all the connection issues. All you need to do as a Servlet developer is what you normally do. Just pretend you're doing one request at a time, which is exactly how it is. Whether it's one socket or four is up to the browser. It is out of your control and care.
1 How does the browser/client know, that he gets the <IMG> content through a keep-alive connection? 2 Does the client still send n requests to the servlet for n images? 3 Does that mean that my servlet has to know the combined length of all n images, and flush the output stream with the n'th image?
1 the client application that talks HTTP knows, but the user doesnt
2 yes, each img/css/html file is a separate request, only with Keep-Alive the connection isn't closed in between each one
3 servlet doesn't have to know the combined size, Keep-Alive just tells the client and server to keep the connection open for more normal requests. Each request is a separate servlet output stream.
Hello Samuel and Nathaniel, Thanks for your help. I read O'Reilly's Servlet Programming again, and I think things are getting clearer now. Setting the contentLength of the response in the servlet gives the client the information it needs to get control over the socket again. One thing is still unclear, when I close the outputStream, does this close the connection as well? Currently I do a outputStream.flush() at the end of the servlet. I am not quite sure with regards to the rendering in the client. Is the browser still suppose to count the missing items (images) down, or would he wait until the whole page is loaded and then show it? I implemented the changes already, the result is not very satisfying, according to the number of open TCP connections I would say the client still opens many connections instead of just two... [ May 25, 2004: Message edited by: Rolf Mueller ]