This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes New lines at the beginning of the JSP output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "New lines at the beginning of the JSP output" Watch "New lines at the beginning of the JSP output" New topic
Author

New lines at the beginning of the JSP output

Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Hi,
Has any one faced any problem with the new line characters at the beginning of the JSP output?(Click view source on the HTML page generated by the JSP, and you will see that the actual output - the <HTML> tag starts after a few new line characters). Well, this is causing a problem for me, and I'd like to get rid of these new line characters. Did any one of you ever face this issue? Any help will be greatly appreciated. Thanks.
-Bala.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can get rid of them if they are really a problem, but it will make your JSP look awful.
The problem is that if you have something like this:

That will send two carriage return to your output.
When you start including multiple JSP tags including scriptlets, it gets worse...

You can remove the returns by wraping your tags:

Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I meant to finish up by saying "the returns disappear because they are now inside the JSP tag rather than being directly output to the client" but I submitted too early...
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

yes -- very ugly -- those extra spaces are put in when your JSP is translated -- you might not be able to get rid of all of them depending on how the translation code is written in your appserver.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

I'm curious, what problems are they causing you? Most browsers won't care about them.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

that's what I was wondering too
seshu kumar
Greenhorn

Joined: Jun 13, 2002
Posts: 23
Browsers are supposed to ignore white space. But in truth it doesnt work that way.
Specially if you have too much white space.
I had a html table being populated by jsp. Where I had significant java code.
All the white space created a empty space of about 15% of screen height above the table tag.
Only way to remove it was to rearrange the jsp as
<% ---Java Code <-cr
%><---- html code.
My jsp looks awful.
Bala Krishna
Ranch Hand

Joined: Jan 26, 2001
Posts: 95
Thanks for all the responses. My problem is very peculiar. I have a server side Java class which 'hits' some JSPs, exchanging data via the request variable, and writes out results to a file. The JSPs do not have any HTML or out.print code. But, the new lines characters returned from the JSPs were introducing new line chars at the beginning of the file, causing some application errors.
Anyways, I've found a simpler and dirty solution to this. Add out.clear() to the code in the JSPs just before you send any output to the browser, and that removes the \ns.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New lines at the beginning of the JSP output