Daniel Joshua <[EMAIL PROTECTED]> writes: > Hi all, > > How do I use HttpServletResponse.setContentLength() to support persistent > (keep-alive) HTTP connections (I heard this helps slightly speed up surfing, > as the same connection is re-used)? > > What is the purpose for Content-Length?
If you set the length of your content to X (you must know the length, of course) then the browser will know to download only X bytes. So if I have, say an HTML file that I want to serve I could do this: public void doGet(HttpServletRequest request, ...) throws IOException { File f = new File("/myhtmlfile.html"); response.setContentLength(f.length()); response.setContentType("text/html"); Reader in = new FileReader(f); Writer out = response.getWrtier(); char buffer[] = new char[5000]; int red = in.read(buffer, 0, 5000); while (red > -1) { out.write(buffer, 0, red); red = in.read(buffer, 0, 5000); } out.close(); in.close(); } If you don't set the content length then the browser will have to either: - guess it - take advantage of server provided chunked encoding HTTP/1.1 mandates chunked encoding in most circumstances. Chunked encoding is really not very onerous so it's not really an overhead, In short, you shouldn't worry about content length. Chunked encoding really solves the problem. Nic ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html