aspose file tools*
The moose likes Servlets and the fly likes keep-alive or persistent connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "keep-alive or persistent connection" Watch "keep-alive or persistent connection" New topic
Author

keep-alive or persistent connection

Rolf Mueller
Greenhorn

Joined: Mar 15, 2004
Posts: 9
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?

Thanks for your help!
Regards,
Rolf
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Samuel M. F. Jackson
Greenhorn

Joined: May 25, 2004
Posts: 2
Originally posted by Rolf Mueller:

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.

--------

Fluidlogic™
Rolf Mueller
Greenhorn

Joined: Mar 15, 2004
Posts: 9
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: keep-alive or persistent connection