tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Corby Jacobs" <cjac...@convera.com>
Subject RE: Whitespace generated by JSTL tags
Date Wed, 13 Nov 2002 21:49:33 GMT
Thanks for explaining the origin of the extra empty lines.  I now see that the 3 lines between
the <option>s match up to the <c:forEach>, <c:if>, and <c:out> tags
in the JSP I used to create the example (see below).

Thanks also for the direction to the filter example.  I will pass it along to the developer.

Perhaps this "preservation of whitespace" feature needs to be in the JSTL FAQ, along with
the filter solution.  I didn't see it anywhere in my travels before I wrote to this list.

Thanks again.


    <tr>
    <td align="left" bgcolor="#ffffff"> 
    This selection menu was generated using standard JSTL tags &lt;c:if&gt;, &lt;c:forEach&gt;,
and &lt;c:out&gt;.
    </td>
    </tr>
    <tr>
    <td align="left" bgcolor="#ffffff"> 
<select name="termselector">
<c:if test="${!empty wordInfo}">
   <c:forEach items="${wordInfo.children}" var="wordNode" varStatus="status">
      <c:if test='${wordNode.text != ""}'>
         <option value="wordNode_<c:out value="${status.count}"/>" testID="option_wordNode_<c:out
value='${status.count}'/>" >
         <c:out value="${wordNode.text}"/>
         </option>
      </c:if>
   </c:forEach>
</c:if>
</select>
    </td>
    </tr>

-----Original Message-----
From: Hans Bergsten [mailto:hans@gefionsoftware.com]
Sent: Wednesday, November 13, 2002 4:38 PM
To: Tag Libraries Users List
Subject: Re: Whitespace generated by JSTL tags


Corby Jacobs wrote:
 > I am investigating an issue with a JSP page which, under certain
 > circumstances, generates a 22.4 MB file to send back to the client.
 > Turns out, a substantial portion of this page is whitespace.  As I
 > see it, the whitespace is coming from two places:
 >
 > 1) whitespace the developer added to make the JSP more readable seems
 > to be repeated in the HTML sent to the client (and duplicated if it's
 > inside a <c:forEach> tag)
 >
 > and
 >
 > 2) the tags themselves are generating extra newline characters
 >
 > To address issue 1, I edited the JSP file, changed all tabs to one
 > blank space, and removed all empty lines.  This reduced the size of
 > the file from 22.4MB to 4.1MB.  Then, within the triple-nested
 > <c:forEach> loops, I made the code all one line -- very ugly, but
 > reduced the size of the HTML file to 367kB.
 >
 > To confirm my suspicions on issue 2, I re-wrote a small excerpt of
 > the page to use scriptlets instead of <c:forEach> and <c:if> tags.
 > The loop is used to generate a <select> with <option>s whose value
 > depend on the iteration through the loop.  As you can see in the
 > excerpt from the "view page" source sent to the client, the version
 > generated with the core JSTL tags has extra whitespace between the
 > <option>s.  Imagine doing this for a menu of 100 <option>s, 3 times,
 > and you can see where all the whitespace is coming from.  I tested
 > this with both Tomcat and Weblogic as the web server.
 >
 > My question is this:  Is there any way to avoid all this whitespace
 > caused by core JSTL tags?
 > [...]

JSP _preserves_ whitespace, so it's not really accurate to say that
tags "generate" whitespace; what you see is the newlines after the
tags being preserved.

Anyway, the best way to handle it is probably to use a filter that
compresses the response (most browsers supports compressed responses
today) since that would reduce the space needed for both whitespace
and repeated tags. See this article for an example of the implementation
of such a filter:

   <http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html>

Another alternative is to use a filter that removes whitespace, but
that requires parsing of the response. You may be able to do it using
something like the W3C JTidy parser, assuming the HTML is basically
well-formed.

   <http://sourceforge.net/projects/jtidy>

Hans
-- 
Hans Bergsten                                <hans@gefionsoftware.com>
Gefion Software                       <http://www.gefionsoftware.com/>
Author of O'Reilly's "JavaServer Pages", covering JSP 1.2 and JSTL 1.0
Details at                                    <http://TheJSPBook.com/>


--
To unsubscribe, e-mail:   <mailto:taglibs-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:taglibs-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-user-help@jakarta.apache.org>


Mime
View raw message