Java Html generation encoed newlines correctly

Hello,

I am generating an html string with some java code, and for some reason the newlines are's kept or encoded correctly. I tried using"\n"

at the end of my lines, and I even tried using :

final String newLine = System.getProperty("line.separator");

with no luck. I am using a StringBuffer

to create the html page, one line at a time with the append(String)

method. I tried writing it (the html file) using the UTF-8 encoding and made sure my html declaired it uses the same encoding, doesn't work. This wouldn't bother me much but when I try to validate the HTML using the w3 validator it has problems with the page, like not detecting the Doctype even though there is one, and I beleive this is why I get other strange errors. Firefox does detect the correct encoding, but when I look at the source using Firefox and IE I have no newlines.

Does anyone have any ideas?

Thanks!

[1025 byte] By [rossettigaba] at [2007-10-3 4:34:29]
# 1

Are you sure that you're getting no newlines? Maybe newlines (\n) are getting added, but on the client machine (Windows, presumably, if you're using IE) it wants newlines preceeded by a carriage return, and isn't interpreting the newlines correctly.

What happens if you do "save page as HTML" on the client and then examine the resulting file with an octal dump type of tool?

paulcwa at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 2
Quick reply, I tried it on IE out of curiosity, I just linux but I get the same thing on both. I'll try the dump and see.
rossettigaba at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 3

Another thing you can try is telnetting to port 80 (or whatever port the server is serving with) on the server (assuming you have a good telnet client) and seeing if the newlines are present.

Or, probably a better option, would be to write a test client in java to connect to the URL in question and see if newlines are in the response content.

Or HttpUnit may be able to do this for you, or at least help with it. I don't know but it's worth checking into.

paulcwa at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 4
Ok, I used a hex dump on the generated file, and there are no newlines. The file was generated by putting "\n" at the end of my strings. Could the StringBuffer be interfering?
rossettigaba at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 5
I also tried your telnet suggestion, and no newlines there either.
rossettigaba at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 6

> Ok, I used a hex dump on the generated file, and

> there are no newlines. The file was generated by

> putting "\n" at the end of my strings. Could the

> StringBuffer be interfering?

Bottom line is, if there aren't any newlines in the output it's because your code isn't writing them there. And StringBuffers don't "interfere", they just store whatever characters you tell them to store. So how about just posting your code?

Oh, and I don't understand why you ask the question. It isn't mandatory for HTML code to have any line ending characters, and generally HTML ignores those that are there.

DrClapa at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 7

Sure, here are parts (the whole thing is too long) :

StringBuffer htmlText = new StringBuffer();

htmlText.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");

htmlText.append("<html>\n");

htmlText.append(" <head>\n");

htmlText.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");

htmlText.append("<!-- #1 Html Title -->\n");

htmlText.append("<TITLE>" + getHtmlTitle() + "</TITLE>\n");

// CSS

htmlText.append(getCss().toHtml());

htmlText.append(" </head>\n");

// More html code...

String html = htmlText.toString();

try {

BufferedReader in = new BufferedReader(new StringReader(html));

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(TMP_WORKING_DIRECTORY_ROOT_DIR +

File.separator + filename), "UTF8"));

String thisLine = "";

while ((thisLine = in.readLine()) != null)

out.write(thisLine);

out.flush();

out.close();

in.close();

} catch (IOException e) {

log.writeToLog(SendPanel.class, "Writing html file failed!");

e.printStackTrace();

}

could it be that my in.readLine() strips the newline/EOL char?

rossettigaba at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...
# 8
I just read the readLine() java doc, and it does strip it....should have thought of that before, it just dawned on mt on my last post. Thanks anyways guys.
rossettigaba at 2007-7-14 22:38:08 > top of Java-index,Java Essentials,Java Programming...