Tips Tricks Samples

What an arse!

I have just wasted almost two hours trying to track down a bug in a servlet I am writing. On the server console I was getting an error going through with every page: "Amount of data written in response is more than specified by Content-Length header". So somewhere I was obviously making a mistake with the StringBuffer which gets returned to the browser. I was just being an unobservant arse with the problem just staring me in the face:
httpservletresponse.setContentLength(sbOut.length());
httpservletresponse.setContentType("text/html");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println(sbOut.toString());
printwriter.close();
should obviously have been
httpservletresponse.setContentLength(sbOut.length());
httpservletresponse.setContentType("text/html");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.print(sbOut.toString());
printwriter.close();
The extra line break returned with the printwriter.println() was making the content length different to that of the contents of the string buffer.

God that's annoying!