views:

56

answers:

3

I've got a "JSP Document" ("JSP in XML") nicely formatted and when the webpage is generated and sent to the user, some linebreaks are removed.

Now the really weird part: apparently the "main" .jsp always gets all its linebreak removed but for any subsequent .jsp included from the main .jsp, linebreaks seems to be randomly removed (some are there, others aren't).

For example, if I'm looking at the webpage served from Firefox and ask to "view source", I get to see what is generated.

So, what determines when/how linebreaks are kept/removed?

This is just an example I made up... Can you force a .jsp to serve this:

<body><div id="page"><div id="header"><div class="title">...

or this:

<body>
  <div id="page">
    <div id="header">
      <div class="title">...

?

I take it that linebreaks are removed to save on bandwidth, but what if I want to keep them? And what if I want to keep the same XML indentation as in my .jsp file?

Is this doable?

EDIT

Following skaffman's advice, I took a look at the generated .java files and the "main" one doesn't have lots of out.write but not a single one writing tabs nor newlines. Contrary to that file, all the ones that I'm including from that main .jsp have lots of lines like:

out.write("\t...\n");

So I guess my question stays exactly the same: what determines how tabs/space/linebreaks are included/removed in the output?

+2  A: 

Not a very precise answer but according to this message, Jasper might be doing this (I admit I didn't check myself):

I had a search around, and don't think this one has been asked before.

I've been writing my JSP pages as XML documents (.jspx suffix), and one difference that annoys me a little between using the terse XML document syntax and the (pre-2.0) legacy syntax are line breaks, or lack of in the former.

The processed XML document results in the XHTML where the tags are unbroken by either spacing or line breaks.

I understand fully the concept behind why this is the case; the first XML document is parsed down to it's node tree, and then the XHTML is generated based on this tree.

However, I am of the understanding that the white spacing doesn't have to be lost along the way. I checked the jasper code, particularly org.apache.jasper.compiler.Node, and a cursory inspection reveals a lot of .trim() calls, that may be unnecessarily removing the spacing. However, I am not so familiar with this code to say decisively.

In summation, this may be a bug/area of improvement. Regardless, is there a way to embed line breaks via configuration? I feel confident to be able to achieve these with ugly, ugly CDATA sections, or some like technique (jsp:text?)... but before I go to that trouble, is there an easier/neater way?

The message is pretty old but the same behavior is also reported in this comment and your question seems to imply it still applies.

Not sure you can change this behavior (without using a Filter).

Pascal Thivent
@Pascal Thivent: +1, thanks a lot for the info :)
NoozNooz42
+3  A: 

As per the JSP specification:

JSP.6.2.3 Semantic Model

...

To clearly explain the processing of whitespace, we follow the structure of the XSLT specification. The first step in processing a JSP document is to identify the nodes of the document. Then, all textual nodes that have only white space are dropped from the document; the only exception are nodes in a jsp:text element, which are kept verbatim. The resulting nodes are interpreted as described in the following sections. Template data is either passed directly to the response or it is mediated through (standard or custom) actions.

So, if you want to preserve whitespace, you need to wrap the desired parts in <jsp:text>.

BalusC
@BalusC: +1 too... That said I tried to wrap in jsp:text and it didn't work (interestingly the person, much more knowledgeable on the subject than me, that asked the question that Pascal Thivent quoted, also asks if jsp:text could work... It is so easy to try that I take it he couldn't make jsp:text to work neither!?). Anyway, +1 too and thanks for the spec quote/link.
NoozNooz42
Which JSP/Servlet container are you using? Which Servlet version is declared in web.xml?
BalusC
@BalusC: Tomcat 6.0.26 (local testing): it was 2.4/JSP spec 2.0, changed to 2.5 and now the <jsp:text> tag works. Thanks a *lot* :)
NoozNooz42
@BalusC: btw I still haven't fully why some .jsp where having their whitespaces/newlines removed and others not but in any case now using jsp:text I can force the behavior I need so it's all good :)
NoozNooz42
You're welcome.
BalusC
A: 
<trim-directive-whitespaces>true</trim-directive-whitespaces>

Whitespace inside <jsp:text> is stripped if you use the above directive in your web.xml. Given the semantic model quoted in BalusC's answer, this directive is only useful if you're not using XML syntax.

(Tomcat 6.0.16; Servlet 2.5 declarations; JSP 2.1 declarations in XML syntax)

McDowell