• Post Reply Bookmark Topic Watch Topic
  • New Topic

Carriage Return Problem

 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

When I use out.write() or out.println() or out.print(), in JSP, the page always contains a number of leading carriage returns:

eg.
(page starts here)






printed content...

This would be OK except that the XML parser doesn't like carriage returns at the beginning and is returning errors. Is there a way to stop the carriage returns?

Any help is greatly appreciated.

Cheers,

Steve
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65520
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thta's just the way JSP works. Any template text in the JSP pages is dutifully transposed to the response. That includes all whitespace.

You could:

1) Make sure there's no whitespace in sensitive areas. Leads to an ugly and fragile JSP.

2) Use a filter to remove the whitespace after the fact.

3) Don't use a JSP.

What exactly are you trying to do? Generate an XML file with a JSP? Better mechanisms exist for that.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Thta's just the way JSP works. Any template text in the JSP pages is dutifully transposed to the response. That includes all whitespace.

You could:

1) Make sure there's no whitespace in sensitive areas. Leads to an ugly and fragile JSP.

2) Use a filter to remove the whitespace after the fact.

3) Don't use a JSP.

4) Use an XML parser that (as it should) ignores whitespace characters.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65520
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he's referring to any whitespace before the <?xml ..> directive. Don't most parsers barf if that's not on the first line?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, my bad.

Have you looked into jspx?
 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

Basically, I'm using JSP to generate an XML document download (not a feed). The JSP basically generates an XML document that can be downloaded and saved like this:

response.setHeader("Content-Disposition", "attachment; filename=\"example.xml\"");

StringBuffer xmlOut = FileAccess.writeDesign(design, true);

out.print(xmlOut.toString());

I'm not all that good at servlets (actually, I don't use them at all...)

Cheers,

Steve

PS The XML parser does seem to be barfing - but only, as you say, if the XML is not in the first line
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65520
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would highly recommend not using a JSP to generate an XML file. As I said, there are much better mechanisms available.

What have you got against servlets?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!